Pages

Monday, October 14, 2013

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.

Friday, August 30, 2013

SUBSTR to replace

SUBSTR can also to used to replace values of a string. In the below example the variable ID, where the first two characters can replaced by XX, with the help of the SUBSTR function

LAST      NAME    ID
Barczak  George  CP436
Adams   Linda     LA543
Preiss    Gloria    AU942
To change the first two letters of the  ID to "XX", use the SUBSTR function on the left side of the assignment statement:
substr(employee_id,1,2)='DA';
Result:
LAST       NAME     ID
Barczak   George   XX436
Adams    Linda      XX543
Preiss     Gloria     XX942

Monday, August 26, 2013

No Warning Messages

When you write a code ensuring no warning messages are thrown in log but this get the error message below.

WARNING 32-169: The quoted string currently being processed has become
                more than 262 characters long.  You may have unbalanced 
                quotation marks.

If so, check out the QUOTELENMAX/NOQUOTELENMAX system option. This option is particularly appropriate when you have long text strings in, for example, the ODS HTML TEXT= statement.