SAS Notes

Compiled by : Wilson Suraweera @ CGHR
Contents
Random Numbers in SAS
Data Sub-setting
Selection  of  two Random Samples
Identification and Separation of duplicate records in SAS dataset
Reshape Long list variable dataset to Wide list dataset
Missing values replacement with Means of the respective Variables
Logistic Regression with SAS
SAS made easy using Proc SQL
SAS PROC SQL procedure to access external ODBC data sources
SAS String Data Handling
SAS Missing value arithmetic's
          Update_a_Table_using_Another_Table_in_SAS
Text analysis - an Epidemiological Case Study by WS - SAS Institute HUG -01 April2011
 
External Resources
SAS Knowledge Base - Glossary of SAS Procedures from SAS.com
SAS resources from - UCLA
Logistic regression
          Survival Analysis : Usage of Proc LifeTest and Proc PHREG
SAS Dinosaur - Old and New way of SAS programming
Paul Dicman's Web Page for SAS- This little old discusses SAS 8, but useful
Global Statements Dictionary - Alphabetical listing and Description of SAS Key words
SAS Study Blog
SAS Canada - User Groups
         

Random numbers in SAS

Random numbers are more useful than you might imagine.  They are used extensively in Monte Carlo studies, as well as in many other situations.  We will look at two of SAS's random number functions.
The statements if x>.5 then coin = 'heads' and else coin = 'tails' create a random variable called coins that has values 'heads' and 'tails'.  The data sets random1 and random2 use a seed value of -1.  Negative seed values will result in different random numbers being generated each time.
DATA random1;      x = UNIFORM(-1);      y = 50 + 3*NORMAL(-1);      IF x>.5 THEN coin = 'heads';        ELSE coin = 'tails';  RUN;     DATA random2;      x = UNIFORM(-1);      y = 50 + 3*NORMAL(-1);      IF x>.5 THEN coin = 'heads';        ELSE coin = 'tails';  RUN;     PROC PRINT DATA=random1;    VAR x y coin;  RUN;  PROC PRINT DATA=random2;    VAR x y coin;  RUN;     OBS       X          Y       COIN   1     0.24441    49.7470    heads     OBS       X          Y       COIN   1     0.16922    49.1155    tails
Sometimes we will want to generate the same random numbers each time so that we can debug our programs. To do this we just enter the same positive number as the seed value.  The data sets random3 and random4 illustrate how to generate the same results each time.
data random3;      x = UNIFORM(123456);      y = 50 + 3*NORMAL(123456);      IF x>.5 THEN coin = 'heads';        ELSE coin = 'tails';  RUN;     data random4;      x = UNIFORM(123456);      y = 50 + 3*NORMAL(123456);      IF x>.5 THEN coin = 'heads';        ELSE coin = 'tails';  RUN;     PROC PRINT DATA=random3;    VAR x y coin;  RUN;  PROC PRINT DATA=random4;    VAR x y coin;  RUN;    OBS       X          Y       COIN   1     0.73902    48.7832    heads     OBS       X          Y       COIN   1     0.73902    48.7832    heads
Now let's generate 100 random coin tosses and compute a frequency table of the results.
DATA random5;    DO i=1 to 100;      x = UNIFORM(123456);      IF x>.5 THEN coin = 'heads';        ELSE coin = 'tails';      OUTPUT;    END;  RUN;     PROC FREQ DATA=random5;    table coin;  RUN;                                 Cumulative 
 Cumulative  COIN    Frequency   Percent   Frequency    Percent  ---------------------------------------------------  heads         48      48.0          48       48.0  tails         52      52.0         100      100.0

3. Problems to look out for

Watch out for math errors, such as division by zero, square root of a negative number and taking the log of a negative number.

4. For more information

For information on functions is SAS consult the SAS Language manual.

Back to Top