Wednesday, 20 July 2011

Fixing Base SAS

"Before I speak, I have something important to say."


Base SAS is dumb. Every time it opens you have to maximize it. What's the point? Like I have time to click stupid buttons when there is important analysis to be done! Fortunately, there is a way to customize how Base SAS starts up. The way to do this is to create a file called AutoExec.sas and save it in the root directory of your local hard drive:


C:\AutoExec.SAS


In the file use the following code to start SAS maximized:


dm 'awsmaximize';


It's also good if you want to automatically run code or assign libraries. I use the include statement to run the scripts that logs me on to our remote server: 


%include "C:\Hi Doggy\SAS Code\Remote Sign On.sas";


What used to be a pointless task is now a dream, I love you again Base SAS.

Date Evaluation

Filtering the date in your data using the today() function in SAS is great, as it will provide you with all the data with today's date.

However filtering for a certain number of days from today's date (e.g. 50 days) using the today() function (e.g. today()-50) can take a while to run, which is not great if your manager needs the data on their desk by the hour and you want to keep your job!!

Using the %eval function can speed up the process:


%let date= %eval(%sysfunc(today())- 50);


The code above creates the macro 'date' using the %eval function to calculate the value of the system function today()-50.

Now all you need to do is filter the date in your data set greater than &date and you'll have all the data you need in no time!

If you don't believe me try using the time it function to test the difference! http://superbasssas.blogspot.com/2011/07/time-it.html

Tuesday, 19 July 2011

IT Help to the Rescue

DATA DOWN!! Probably the worst nightmare for any analyst, but not when you have the amazing IT Help Desk at hand, after initially not picking up the phone the following conversation took place:


ITHelp                           you rang ...
Mark                              hey
Mark                              yeah just to let you know we have no data
ITHelp                           that's nice
Mark                              lol.....not really
Mark                              might be useful to have
Mark                              well apparently it doesnt exist now
ITHelp                           some people want it all
Mark                              yeah, these analysts huh......who would think that they actually need ................data
ITHelp                           that's just what is wrong with society today
Mark                              haha
Mark                              do you know what happened to the table?
ITHelp                           black hole?
ITHelp                           whirl pool?
ITHelp                           gap in the space time continuum
ITHelp                           ok sorry, I will have a look
Mark                              LOL
ITHelp                           just getting my dry suit on now
Mark                              haha, cool, be careful.......those gaps in the space time continuum ................can be tricky
ITHelp                           it' ok I've got my light saber
Mark                              may the force be with you
ITHelp                           I think that the job that updates the data has gotten stuck
ITHelp                           I should have noticed it earlier but was too busy eating scones - my ................apologies
Mark                              what did you eat the scones with?........thanks for letting me know, do ................you know when it will be ready?
ITHelp                           strawberry jam
ITHelp                           home made too
ITHelp                           I am going to have to stop and restart the server in order to sort this ................out - this will kill everybodys eg projects that are running
ITHelp                           how does that sound?
Mark                              errmmmm great to me, dunno about them...but who cares

After kicking everyone of the server normality was restored, thanks once again to the IT Help Desk!

Time it!

"Time flies like an arrow; fruit flies like a banana"

Often when writing code it's important to know how long it will take to run. This can be very useful when creating loops that need to be repeated hundreds of times or when working out which bits of code are slowing everything else down.

The following code will work out how long something takes to run and output the time into the log:

%let tic = %sysfunc(time(),8.); 

*Insert code here*

%let toc = %sysfunc(time(),8.);

%put Total Time %sysfunc(putn(%eval(&toc-&tic),time8.));


The code works by storing the system times into the macro variables 'tic' and 'toc'. The difference between the two will tell you how long the code took to run.  Here the 'Total Time' taken is printed into the log, but it can just as easily be saved into a data set for later viewing.


Mark Meat.

Monday, 18 July 2011

Creating A Macro Using Data From An Existing Table!

This is a simple piece of code that I use in most of the projects that I create.


PROC SQL noprint;
select distinct age into :agelist separated by ',' from agedataset;
quit;

The code above takes all of the ages from the data set conveniently called agedataset, and places them into a macro (also conveniently) called agelist. This list of ages within the macro is then separated via commas (',').

Now if I want to filter another table for only the ages within the agedataset, all I need to do is filter for ages IN &agelist and SAS will do the rest for me!

Welcome to SuperBass SAS!!

SuperBass SAS has been created to share some of the useful code that we as SAS analysts use daily. This can be anything from a simple macro to using hash tables, anything that makes our lives as analysts simpler that we sometimes take for granted.

Please feel free to add any code that you yourself are using, also if you see anything on this blog that you could make more efficient, please share this with us, so that we can all learn and become better analysts!!

Enjoy;