Pages

Monday, October 14, 2013

Automatic email from SAS after program completion

 If you want to sent an email as soon as your program completes, try:

sas program.sas ; echo program.sas is done | mail youruserid

If you don't want to regularly check the status of the program and wants to get notified to start your next job
If you only want an email if your program bugs out, try:

sas program.sas || echo your program didn't work | mail youruserid

You can write anything that you want in your email after echo.

Limit the size of dataset for test

Sometime datasets are too large and to run the test on those datasets to check if the program is working fine, it takes a lot of time.

You can overcome this by OBS = option: Here is an example:


Example:
data sub85;
set lib.all85 (obs=1000);
proc glm data=sub85;
model icd90=female black state1-stat49;


Again, you can also use this as a system option:

options obs=1000 [other SAS system options here];

You can remove it once the program is running smoothly.

Thursday, October 10, 2013

Order of execution in multiple dataset options?

When multiple dataset options are applied to a single statement, then the order of execution matters. The rule of thumb is that they are executed according to the alphabetical order of their names. That is, a drop option is executed before a keep, and a keep before a rename.
Now, given also that the variable number is decided by the order of first-appearance in the data step, can you guess the output of the code snippet below? 

data one;
  set sashelp.class(rename=(name=eman) keep=age sex name drop=age);
run;
 
/* check */
proc contents data=one varnum;
run;

Wednesday, October 9, 2013

How to check for the existence of a directory in SAS?

Did you ever encountered with an issue where you need to check the existence of a directory with a SAS code. Here is the code which let you do the same.

If directory exists macro will return 1 else 0.

%macro DirExist(dir) ; 
   %LOCAL rc fileref return; 
   %let rc = %sysfunc(filename(fileref,&dir)) ; 
   %if %sysfunc(fexist(&fileref))  %then %let return=1;    
   %else %let return=0;
   &return
%mend DirExist;


/* Usage */
option noxwait; /* SAS 8.2 only */
%put %DirExist(C:\Documents and Settings\);
%put %DirExist(Test);

Aligning Character Values with a Put Function

We often use LEFT and RIGHT SAS functions to align the character values. This can also be achieved by PUT function.

Let us see this with and example:

data temp;
 
alphabetw='  a  ';
chardate=put(alphabetw,$10. -l);
output ;
chardate=put(alphabetw,$10. -c);
output ;
chardate=put(alphabetw,$10. -r);
output ;
 
run;


proc print;run;
 
Obs    alphabetw  chardate
 
 1        a       a
 2        a           a
 3        a                a

Monday, October 7, 2013

How to label the variable created by PROC TRANSPOSE

In Proc Transpose, you can tell SAS, how to NAME the new variable created from the result of Proc Transpose.
Also  by using the the value of the variable from the original dataset, you can tell SAS to LABEL the variables.
Below is the example demonstrating the same.
  proc transpose data=test out=test2 prefix=visit;
    by subjid;
    var labres;
    id visit;
    idlabel visitl;
  run;
Based on the values in the VISIT variable, SAS will name the variables VISITx .. VISITy. And will ALSO label VISITx with the corresponding value from VISITL on the given record and label VISITy with the corresponding value from VISITL on the given record for all created variables.