Skip to main content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

SAS

ATTENTION

ATTENTION: With the departure of Dr. Raeda Anderson from GSU July 2020, the workshop materials and information on this guide will no longer be actively supported.  These materials will be kept online for the time being.  For questions about these materials, or to inquire about currently supported workshops and subject-areas, please visit the Research Data Services homepage: https://research.library.gsu.edu/dataservices

Workshop Materials

On this page you can find the workshop files for the "SAS" workshop series.  In order to engage with the workshop materials directly, please go through the following steps:

  1. Download and install SAS: https://technology.gsu.edu/technology-services/it-services/software-computer-purchase/software-download-and-purchase/
  2. Download workshop files from the appropriate link in the tabbed section below.

Recorded versions of all Research Data Services workshops can be found at the following link: https://research.library.gsu.edu/dataservices/rds-workshops-recordings

Workshop Data & Handouts

DATA

age child ichild class degree housing cappun mari gun happy

47           2              3              3              3              1              1              1              1              2

61           2              0              2              4              1              1              1              1              2

72           1              2              2              3              1              1              2              1              1

43           1              4              2              3              1              1              1              2              2

55           2              2              1              3              1              1              1              1              1

53           2              2              2              3              2              1              1              1              1

50           1              2              3              3              1              1              2              1              2

23           2              3              2              2              1              1              2              1              1

45           3              3              2              1              1              2              2              1              2

71           1              4              3              3              2              1              2              1              2

33           2              5              4              3              1              2              1              1              3

86           2              4              3              3              1              1              2              1              3

32           2              3              3              2              1              2              1              1              1

60           2              5              2              3              1              1              1              1              1

76           1              7              2              3              2              2              2              2              2

33           1              2              3              2              1              2              1              1              2

56           1              6              1              2              1              2              1              1              1

62           2              5              2              1              2              2              1              2              2

31           2              0              2              2              1              2              1              2              2

43           2              2              3              1              1              2              1              1              2

58           1              0              3              3              1              1              1              2              2

32           1              3              2              2              2              2              1              2              2

23           1              2              2              2              2              2              2              1              1

37           1              2              3              2              1              2              1              1              1

25           1              2              4              2              2              2              2              1              2

76           2              8              4              2              1              2              2              1              3

71           1              2              3              3              2              1              1              1              1

22           2              0              3              3              1              2              1              2              3

74           1              2              2              4              2              1              2              2              2

75           1              3              1              3              1              2              1              1              3

HANDOUT

CODE

***** PULLING IN DATA;

DATA sas1;

INPUT age child ichild class degree housing cappun mari gun happy

 ;

DATALINES;

47           2              3              3              3              1              1              1              1              2

61           2              0              2              4              1              1              1              1              2

72           1              2              2              3              1              1              2              1              1

43           1              4              2              3              1              1              1              2              2

55           2              2              1              3              1              1              1              1              1

53           2              2              2              3              2              1              1              1              1

50           1              2              3              3              1              1              2              1              2

23           2              3              2              2              1              1              2              1              1

45           3              3              2              1              1              2              2              1              2

71           1              4              3              3              2              1              2              1              2

33           2              5              4              3              1              2              1              1              3

86           2              4              3              3              1              1              2              1              3

32           2              3              3              2              1              2              1              1              1

60           2              5              2              3              1              1              1              1              1

76           1              7              2              3              2              2              2              2              2

33           1              2              3              2              1              2              1              1              2

56           1              6              1              2              1              2              1              1              1

62           2              5              2              1              2              2              1              2              2

31           2              0              2              2              1              2              1              2              2

43           2              2              3              1              1              2              1              1              2

58           1              0              3              3              1              1              1              2              2

32           1              3              2              2              2              2              1              2              2

23           1              2              2              2              2              2              2              1              1

37           1              2              3              2              1              2              1              1              1

25           1              2              4              2              2              2              2              1              2

76           2              8              4              2              1              2              2              1              3

71           1              2              3              3              2              1              1              1              1

22           2              0              3              3              1              2              1              2              3

74           1              2              2              4              2              1              2              2              2

75           1              3              1              3              1              2              1              1              3

;

RUN;

 

***** EXPLANATION PULLING IN DATA;

** DATA dataname;

** INPUT var1 var2 var3 var4 var5 var6 var7 var8 var9 var10;

** var1res1         var2res1               var3res1               …             var10res1

** var1res2         var2res2               var3res2               …             var10res2

** var1res3         var2res3               var3res3               …             var10res3

** var1res4         var2res4               var3res4               …             var10res4

** …

** var1res10      var2res10            var3res10            …             var10res10

 

** ;

** RUN;

 

***** VIEW DATA IN A TABLE;

PROC PRINT DATA=sas1(obs=10);

RUN;

 

PROC PRINT DATA=sas1(obs=30);

RUN;

 

***** EXPLANATION VIEW DATA IN A TABLE;

** PROC PRINT DATA=dataname(obs=#);

**           RUN;

 

** PROC PRINT DATA= dataname (obs=#);

**           RUN;

 

***** FREQUENCY TABLES OPTION 1 (INDIVIDUAL COMMANDS);

PROC FREQ DATA=sas1;

TABLES age;

RUN;

 

PROC FREQ DATA=sas1;

TABLES child;

RUN;

 

PROC FREQ DATA=sas1;

TABLES ichild;

RUN;

 

***** EXPLANATION FREQUENCY TABLES OPTION 1 (INDIVIDUAL COMMANDS);

** PROC FREQ DATA= dataname;

**           TABLES var1;

**           RUN;

 

** PROC FREQ DATA= dataname;

**           TABLES var2;

**           RUN;

 

** PROC FREQ DATA= dataname;

**           TABLES var3;

**           RUN;

 

***** FREQUENCY TABLES OPTION 2 (ONE COMMAND);

PROC FREQ DATA=sas1;

TABLES age child ichild;

RUN;

 

***** EXPLANATION FREQUENCY TABLES OPTION 2 (ONE COMMAND);

** PROC FREQ DATA= dataname;

**           TABLES var1 var2 var3;

**           RUN;

 

 

***** DESCRIPTIVE STATISTICS OVERALL;

PROC MEANS DATA=sas1;

RUN;

 

***** EXPLANATION DESCRIPTIVE STATISTICS OVERALL;

** PROC MEANS DATA= dataname;

**           RUN;

 

***** DESCRIPTIVE STATISTICS BY A SPECIFIC VARIABLE THAT IS CATEGORICAL;

PROC MEANS DATA=sas1;

                CLASS cappun;

RUN;

 

***** EXPLANATION DESCRIPTIVE STATISTICS BY A SPECIFIC VARIABLE THAT IS CATEGORICAL;

** PROC MEANS DATA= dataname;

**           CLASS var1;

**           RUN;

 

***** DESCRIPTIVE STATISTICS BY A SPECIFIC VARIABLE THAT IS CONTINUOUS ;

** PROC UNIVARIATE DATA= sas1;

**           VAR age;

**           RUN;

 

***** EXPLANATION DESCRIPTIVE STATISTICS BY A SPECIFIC VARIABLE THAT IS CONTINUOUS ;

** PROC UNIVARIATE DATA= dataname;

**           VAR var1;

**           RUN;

 

***** DESCRIPTIVE STATISTICS BY A SPECIFIC VARIABLE THAT IS CONTINUOUS BY EACH VALUE;

PROC FREQ DATA=sas1;

                TABLES child;

RUN;

 

***** EXPLANATION DESCRIPTIVE STATISTICS BY A SPECIFIC VARIABLE THAT IS CONTINUOUS BY EACH VALUE;

** PROC FREQ DATA= dataname;

**           TABLES var1;

**           RUN;

 

***** CROSSTABULATION;

PROC FREQ DATA=sas1;

                TABLES mari * gun;

RUN;

 

***** EXPLANATION CROSSTABULATION;

** PROC FREQ DATA= dataname;

**           TABLES var1 * var2;

**           RUN;

 

 

***** CROSSTABULATION WITHOUT SPECIFIC COMPONENTS (ORDER DOESN'T MATTER);

PROC FREQ DATA=sas1;

TABLES mari * gun / NOROW NOCOL NOFREQ;

RUN;

 

***** EXPLANATION CROSSTABULATION WITHOUT SPECIFIC COMPONENTS (ORDER DOESN'T MATTER);

** PROC FREQ DATA= dataname;

**           TABLES var1 * var2 / NOROW NOCOL NOFREQ;

**           RUN;

** NOTE: NOROW= no row percentages, NOCOL= no column percentages, NOFREQ= no frequencies per cell so the above code just gives percent’s per cell

 

***** BAR CHART;

TITLE 'Bar Chart for Ideal Number of Children;

PROC GCHART DATA=sas1;

VBAR ichild/ DISCRETE;

RUN;

 

***** EXPLANATION BAR CHART;

** TITLE 'Name for Bar Chart’;

**           PROC GCHART DATA= dataname;

**           VBAR var1/ DISCRETE;

**           RUN;

 

 

***** CORRELATION;

PROC CORR DATA = sas1 ;

                VAR ichild child age;

RUN;

 

***** EXPLANATION CORRELATION;

** PROC CORR DATA = dataname;

**           VAR var1 var2 var3;

**           RUN;

 

 

***** REGRESSION;

PROC REG DATA = sas1;

                MODEL ichild = child age housing happy;

RUN;

 

***** EXPLANATION REGRESSION;

** PROC REG DATA = dataname;

**           MODEL dependentvar = independentvar1 independentvar2 independentvar3 independentvar4;

**           RUN;

 

 

***** ANALYSIS OF VARIANCE;

PROC GLM DATA=sas1;

                CLASS gun;

                MODEL age = gun;

RUN; 

 

***** EXPLANATION ANALYSIS OF VARIANCE;

** PROC GLM DATA= dataname;

**           CLASS var1;

**           MODEL var2 = var1;

**           RUN; 

DATA

age child ichild hrs educ evcrack

61    0     2     42    12    1

53    2     2     .     14    2

33    5     4     .     12    2

32    3     8     39    12    2

76    7     2     .     10    2

31    0     2     40    18    2

58    0     3     40    12    2

23    2     2     15    10    2

76    8     4     .     3     2

22    0     3     4     14    2

32    0     2     .     12    2

35    0     2     40    12    2

59    2     2     25    16    2

79    1     3     .     14    2

68    2     2     6     16    2

70    0     2     .     17    2

35    0     2     44    16    2

47    0     8     50    16    2

25    0     2     50    16    2

20    0     3     20    13    2

42    2     3     35    16    2

40    3     3     40    17    2

57    2     2     40    17    2

35    2     5     .     12    2

39    3     8     .     13    2

29    2     3     40    12    2

70    4     5     .     12    2

45    0     8     40    16    2

57    3     2     .     12    2

58    0     2     99    15    2

43    0     2     98    16    2

25    0     2     45    16    2

66    4     2     .     12    2

48    0     3     35    14    2

32    0     8     60    20    2

59    0     3     45    19    2

69    3     2     .     12    2

71    2     2     .     16    2

21    0     3     .     15    2

33    0     2     40    16    2

68    0     4     .     12    2

62    0     2     40    14    2

51    2     2     40    16    2

47    2     2     60    14    2

57    1     2     40    12    2

25    1     4     40    13    2

33    1     3     40    15    2

57    2     8     .     13    2

22    3     3     60    11    2

20    1     7     20    12    2

HANDOUT

CODE

***** PULLING IN DATA; 

DATA sas2;

INPUT age child ichild hrs educ evcrack;

DATALINES;

61 0 2 42 12 1

53 2 2 . 14 2

33 5 4 . 12 2

32 3 8 39 12 2

76 7 2 . 10 2

31 0 2 40 18 2

58 0 3 40 12 2

23 2 2 15 10 2

76 8 4 . 3 2

22 0 3 4 14 2

32 0 2 . 12 2

35 0 2 40 12 2

59 2 2 25 16 2

79 1 3 . 14 2

68 2 2 6 16 2

70 0 2 . 17 2

35 0 2 44 16 2

47 0 8 50 16 2

25 0 2 50 16 2

20 0 3 20 13 2

42 2 3 35 16 2

40 3 3 40 17 2

57 2 2 40 17 2

35 2 5 . 12 2

39 3 8 . 13 2

29 2 3 40 12 2

70 4 5 . 12 2

45 0 8 40 16 2

57 3 2 . 12 2

58 0 2 99 15 2

43 0 2 98 16 2

25 0 2 45 16 2

66 4 2 . 12 2

48 0 3 35 14 2

32 0 8 60 20 2

59 0 3 45 19 2

69 3 2 . 12 2

71 2 2 . 16 2

21 0 3 . 15 2

33 0 2 40 16 2

68 0 4 . 12 2

62 0 2 40 14 2

51 2 2 40 16 2

47 2 2 60 14 2

57 1 2 40 12 2

25 1 4 40 13 2

33 1 3 40 15 2

57 2 8 . 13 2

22 3 3 60 11 2

20 1 7 20 12 2

;

RUN;

 

***** EXPLANATION PULLING IN DATA;

** DATA dataname;

** INPUT var1 var2 var3 var4 var5 var6 var7 var8 var9 var10;

** var1res1 var2res1 var3res1 … var10res1

** var1res2 var2res2 var3res2 … var10res2

** var1res3 var2res3 var3res3 … var10res3

** var1res4 var2res4 var3res4 … var10res4

** …

** var1res10 var2res10 var3res10 … var10res10

 

** ;

** RUN;

 

***** VIEW DATA IN A TABLE;

PROC PRINT DATA=sas2;

RUN;

 

***** EXPLANATION VIEW DATA IN A TABLE;

** PROC PRINT DATA=dataname;

**  RUN; 

 

***** FREQUENCY TABLES (INDIVIDUAL COMMAND);

 

proc freq data=sas2; 

tables hrs; 

run; 

 

***** EXPLANATION FREQUENCY TABLES (INDIVIDUAL COMMAND);

** PROC FREQ DATA= dataname;

**  TABLES var1;

**  RUN;

 

***** DESCRIPTIVE STATISTICS OVERALL;

proc means data=sas2; 

run; 

 

***** EXPLANATION DESCRIPTIVE STATISTICS OVERALL;

** PROC MEANS DATA= dataname;

** RUN; 

 

 

***** GENERATING NEW VARIABLES; 

* Making a new variable with no preexisting data

Variable Made = wave1 with all respondents having a 1; 

Data sas2; 

SET sas2; 

wave1=1; 

RUN; 

 

* EXPLANATION MAKING A NEW VARIABLE WITH NO PREEXISTING DATA

Variable Made = wave1 with all respondents having a 1; 

** Data dataname; 

** SET dataname; 

** newvariablename=1; 

** RUN; 

 

PROC FREQ DATA=sas2; 

TABLES wave1; 

RUN;

 

* COPYING AN OLD VARIABLE

Variable Made= copy of if respondent has ever used crack; 

 

DATA sas2;

SET sas2;

crackuse = evcrack;

RUN; 

 

* EXPLANATION COPYING AN OLD VARIABLE

Variable Made= copy of if respondent has ever used crack; 

 

** DATA dataname;

** SET dataname;

** newvariablename = oldvariablename;

** RUN; 

 

PROC FREQ DATA=sas2; 

TABLES crackuse; 

RUN;

 

* MAKING A DUMMY VARIABLE BY RECODING EACH VALUE

Variable Made = haskid where 1= the respondent has a child and 0= respondent does not have a child; 

 

DATA sas2;

SET sas2;

haskid = .;

IF (child=0) THEN haskid = 0;

IF (child=1) or (child=2) or (child=3) or (child=4) or 

(child=5)or (child=6) or (child=7) or (child=8)

THEN haskid = 1;

RUN; 

 

* EXPLANATION MAKING A DUMMY VARIABLE BY RECODING EACH VALUE

Variable Made = haskid where 1= the respondent has a child and 0= respondent does not have a child; 

 

** DATA dataname;

** SET dataname;

** newvariablename = .;

** IF (oldvariablename=0) THEN newvariablename = 0;

** IF (oldvariablename =1) or (oldvariablename =2) or

** (oldvariablename =3) or (oldvariablename =4) or 

** (oldvariablename =5)or (oldvariablename =6) or

** (oldvariablename =7) or (oldvariablename =8)

** THEN newvariablename = 1;

** RUN; 

 

PROC FREQ DATA=sas2; 

TABLES haskid; 

RUN; 

 

* MAKING A DUMMY VARIABLE BY COLLAPSING VALUES

Variable Made= fulltime where 1= respondent worked 40 hours or more last week ; 

 

DATA sas2;

SET sas2;

fulltime = .;

IF (hrs<40) THEN fulltime = 0;

IF (hrs>=40)THEN fulltime = 1;

RUN; 

 

* EXPLANATION MAKING A DUMMY VARIABLE BY COLLAPSING VALUES

Variable Made= fulltime where 1= respondent worked 40 hours or more last week ; 

 

** DATA dataname;

**  SET dataname;

**  newvariablename = .;

**  IF (oldvariablename <40) THEN newvariablename = 0;

** IF (oldvariablename >=40)THEN newvariablename = 1;

** RUN; 

 

PROC FREQ DATA=sas2; 

TABLES fulltime; 

RUN;

 

* MAKING A VARIABLE FROM MULTIPLE VARIABLES 

Variable Made: childdif for difference between number of children and

ideal number of children

0= respondent has their ideal number of children

positive= respondent has more children than their ideal number of 

children 

negative = respondent wants more children than they have; 

 

 DATA sas2;

SET sas2;

childdif = child-ichild;

RUN;

 

* EXPLANATION MAKING A VARIABLE FROM MULTIPLE VARIABLES 

Variable Made: childdif for difference between number of children and

ideal number of children

0= respondent has their ideal number of children

positive= respondent has more children than their ideal number of 

children 

negative = respondent wants more children than they have; 

 

** DATA dataname;

** SET dataname;

** newvariablename = oldvariablename1- oldvariablename2;

** RUN;

 

PROC FREQ DATA=sas2; 

TABLES childdif; 

RUN;

 

* NOTE you can use any simple mathematical function to generate variables. 

 

subtraction: var1 - var2

addition: var1 + var2

multiplication: var1 * var2

division: var1 / var2

 

I strongly suggest you make small changes and check each step rather

than making all of the changes at once.

 

So if you want to add two variables then multiply by another variable I 

suggest the following

 

*Step 1: add the variables

*DATA dataname;

*SET dataname;

*var3 = var1 + var2;

*RUN;

 

*Step 2: multiply the variables 

*DATA dataname;

*SET dataname;

*var3 * var4;

*RUN;

 

***** LABELING 

 

* VARIABLE NAMES; 

DATA sas2;

   SET sas2;

   LABEL age = "age in years"

    child = "number of living children"

childdif   = "difference between number of children and

   ideal number of children"

crackuse = "respondent has used crack=2"

educ = "years of education"

evcrack = "respondent has used crack=2"

fulltime   = "respondent works fulltime=1" 

haskid = "respondent has child =1"

ichild = "ideal number of children"

wave1 = "respondent in wave 1" 

           ;

RUN;

 

* EXPLANATION VARIABLE NAMES; 

** DATA dataname;

**   SET dataname;

**   LABEL variable1 = "variable 1 label"

**    variable2 = "variable 2 label"           ;

** …

** variableN = "variable N label"

** RUN;

 

PROC CONTENTS DATA=sas2; 

RUN;

 

PROC FREQ DATA= sas2; 

TABLES crackuse haskid fulltime; 

 

RUN;

 

* VARIABLE VALUES; 

PROC FORMAT; 

  VALUE   crackuselab 1="no"

                 2="yes" 

;

  VALUE  haskidlab 0= "no"

    1= "yes" 

;

  VALUE  fulltimelab 0= "no"

1= "yes" 

;

RUN;

 

PROC FREQ DATA= sas2; 

FORMAT crackuse crackuselab.

haskid haskidlab.

fulltime fulltimelab.;

 

TABLES crackuse haskid fulltime; 

 

RUN; 

 

* EXPLANATION VARIABLE VALUES; 

** PROC FORMAT;

**  VALUE  variable1  1="value for variable1"

**                 2="value for variable1" 

** ;

**  VALUE  variable2  0="value for variable2"

**                 1="value for variable2" 

** ;

**  VALUE  variable3  0="value for variable3"

**                 1="value for variable3" 

** ;

** RUN;

 

** PROC FREQ DATA= sas2; 

** FORMAT variable variable1. 

** variable variable2.

** variable variable3.;

 

** TABLES variable variable variable; 

 

** RUN; 

 

***** OUTPUTTING DATA AND RESULTS

 

* DATA TO WORD; 

ods rtf file='sas2.rtf';

 

PROC PRINT DATA=sas2;

    RUN;

 

ods rtf close;

 

* EXPLANATION DATA TO WORD; 

** ods rtf file='datafilename.rtf';

 

** PROC PRINT DATA=dataname;

**    RUN;

 

** ods rtf close;

 

 

* RESULTS TO WORD; 

ods rtf file= "C:\Users\randerson39\Documents\SAS Tutorial\1.rtf";

 

PROC FREQ DATA=sas2; 

TABLES fulltime; 

RUN;

 

ods rtf close;

 

* EXPLANATION RESULTS TO WORD; 

** ods rtf file= "computer path to where you want the data saved\filename.rtf";

 

**code for the analysis you want to make for the word document- this can be a single table or the entire output;

 

** ods rtf close;