Available Immediately
603-275-6809


Return to Homepage

Other interesting pages ...
Resume
SAS Cheat Sheet
Useful SAS Code
Full SAS Example
Basic Statistics
Contact Information

SAS Tip of the Month
June 2008

Some of you may not have seen the tips for this year due to a server issue with my web page host. This has been corrected, and for those of you who missed them, here are all the tips from January to June.

I am off that PharmaSUG this month presenting two papers. While there I will be learning what others are doing and noting tricks with the code that they are doing.

This leads me to this month's tip - when you are working, have a notebook nearby that you can use to make notes of something "wow" you do or see in others' programs. You will be surpised the number of times you will refer to it in the future when writing your own code.

May 2008

When you are adding values from two or more variables it is best to use the sum function rather than the '+' operator as the latter will not deal with missing values, as the following example shows:

    141  data testdata;
    142     infile cards;
    143     input a b;
    144     c=a+b;
    145     d=sum(a,b);
    146     put a= b= c= d=;
    147  cards;
   
    a=3 b=5 c=8 d=8
    a=6 b=. c=. d=6
    a=. b=4 c=. d=4
    a=. b=. c=. d=.

In the first case, variables c and d gave the correct value, but in the next two cases use of the SUM function correctly treated the non missing values.

April 2008

When data is transposed it is possible to put both a label onto the transpsed variable inside the TRANSPOSE procedure. The following code shows how this is done:

    DATASET: VITAL_IN
    SUBJECT  TEST  TSTLABEL     TSTVALUE
     00-01   HGT   Height (cm)  175
     00-01   WGT   Weight (kg)  75
   
    proc sort data=vital_in;
       by subject;
    run;
    proc transpose data=vital_in out=vital_out (drop=_name_);
       by subject;
       id test;
       idlabel tstlabel;
       var tstvalue;
    run;
    options nocenter;
    proc print data=vital_out label;
    run;
   
                      Height    Weight
    Obs    subject     (cm)      (kg)
     1      00-01       175       75

March 2008

I sometimes want to get a listing of all the SAS programs for a particular directory. There are a large number of solutions available, but here is my favourite using PIPE within SAS:

    filename dircmd pipe "dir /b C:\TUAI\TABLES";
    data dirfile;
       length filelst $256;
       infile dircmd missover length=len;
       input filelst $varying246. len;
    run;

Febuary 2008

I had an occasion recently to need negative values to to be displayed with a '()' around the value, rather than the more usual '-' in front of the value. Below is the code that was used to do this and a sample of its use:

     proc format;
        picture negvals
           low-<0='00000.0)' (prefix='(')
           0-high='00000.0' ;
     run;
      
     72   data _null_;
     73      do pctchng= 50 to -50 by -20;
     74         dispval=put(pctchng,negvals.);
     75         put pctchng= dispval=;
     76      end;
     77   run;
      
     pctchng=50 dispval=50.0
     pctchng=30 dispval=30.0
     pctchng=10 dispval=10.0
     pctchng=-10 dispval=(10.0)
     pctchng=-30 dispval=(30.0)
     pctchng=-50 dispval=(50.0)

January 2008

When looking at a SAS program you are likely to see something like the following when dealing with text strings:

    rslt_string=TRIM(LEFT(original_string));

What this did was left align the text in the variable original_string and then remove any blanks after the last character.

In SAS 9 a new function was introduced called STRIP that does the same task but with one function so the original code above can instead be written as:

    rslt_string=STRIP(original_string);

If you are still using an older version of SAS you could do the same thing with a macro, using the following code:

    %MACRO STRIP(intxt); /*INTXT=text variable*/
        TRIM(LEFT(&intxt))
    %MEND STRIP;
    rslt_string=%STRIP(original_string);

You may wonder why there is no ';' at the end of the code inside the macro definition? This allows for the macro to be called inside the code multiple times, as the following example demonstrates:

    rslt_string=%STRIP(original_string_A)||%STRIP(original_string_B);

Updated January 16, 2008