SAS Workshop Introduction to SAS Programming Iowa State University

advertisement
SAS Workshop
Introduction to SAS
Programming
DAY 2 SESSION I
Iowa State University
May 10, 2016
Sample SAS Program B4
libname mylib "U:\Documents\SAS_Workshop_Spring2016\Data\";
data mylib.world;
infile "U:\Documents\SAS_Workshop_Spring2016\Data\demogr.txt";
input Country $20. Birthrate Deathrate InfMort LifeExp PopUrban PercGNP
LevTech CivilLib;
if InfMort<24 then Infgrp=1;
else if 24<=InfMort<74 then Infgrp=2;
else if InfMort>=74 then Infgrp=3;
if LevTech<13 then TechGrp=1;
else if 13<= LevTech<25 then TechGrp=2;
else if 25<= LevTech<60 then TechGrp=3;
else if LevTech>= 60 then TechGrp=4;
label Birthrate='Crude Birth Rate'
InfMort='Infant Mortality Rate'
LifeExp='Life Expectancy in Years'
PercGNP='Per capita GNP'
PopUrban='Percent of Urban Population’;
label Infgrp='Infant Mortality Rate'
Techgrp='Level of Technology';
run;
ODS Graphics (or Template-based Graphics)
 ODS Graphics is typically enabled by default in the SAS windowing






environment.
By default, the Use ODS Graphics box is checked to in the
Tools>Options>Preferences to enable ODS Graphics .
When ODS Graphics is enabled, SAS procs that support ODS
Graphics create graphs that are in standard image file formats.
Their appearance and layout are controlled by ODS styles and templates.
Graphics (and other output, such as tables produced by the procedure)
may be sent to a destination such as a pdf or an rtf le
SAS 9.4 uses the default style called HTMLBlue available for the HTML
destination (which is the default format for output).
Template-based graphs are produced as default output from SAS
procs or when you specify options or use statements for requesting
specific graphs.
Sample SAS Program B5
libname mylib "U:\Documents\SAS_Workshop_Spring2016\Data\";
proc univariate data=mylib.world normal cibasic mu0=70 3000 60;
var LifeExp PercGNP PopUrban;
id Country;
probplot LifeExp PercGNP PopUrban/normal(mu=est sigma=est);
title 'Distributions of Life Expectancy, Per capita GNP,
Urban Population';
run;
proc univariate data=mylib.world noprint;
var PercGNP PopUrban ;
histogram PopUrban/ normal;
histogram PercGNP/ midpoints=0 to 16000 by 1000 lognormal;
run;
User Defined Formats: Proc Format
 Proc Format is a SAS procedure that allows the user to define
formats to do specialized conversions for output.
 In SAS Program B6 we access the SAS dataset world previously created




and produce a histogram of Urban Population super-imposed with
density curves and a scatter plot matrix of 6 variables.
Note the use of user-defined formats for defining more descriptive names
for the levels of the TechnGrp variable.
In the proc format step, the value statement gives a name to the
format and defines a set of values to be output for each of possible
value of the variable for which the format is to be used.
In this example, we used the format name tg for formatting the values
of the 4 possible values of the variable TechGrp.
We then used a format statement to assign the format tg. for printing
values of the TechGrp variable.
Sample SAS Program B6
libname mylib "U:\Documents\SAS_Workshop_Spring2016\Data\";
proc sgplot data=mylib.world;
title "Distribution of Urban Population: Demographic Data";
histogram PopUrban;
density PopUrban;
density PopUrban / type=kernel;
keylegend / location=inside position=topright;
run;
proc format;
value tg 1='Low'
2='Moderate'
3='High'
4='Very High' ;
run;
proc sgscatter data=mylib.world;
title "Scatterplot Matrix for Demographic Data";
matrix Birthrate Deathrate InfMort LifeExp
PopUrban PercGNP/group=Techgrp;
format TechGrp tg.;
run;
Sample SAS Program B7
data emissions;
input period @;
hc=1;
do until (hc<0);
input hc @;
if (hc<0) then return;
output;
end;
label period = 'Year of Manufacture'
hc = 'Hydrocarbon Emissions (ppm)';
datalines;
1 2351 1293 541 1058 411 570 800 630 905 347 -1
2 620 940 350 700 1150 2000 823 1058 423 270 900 405 780 -1
3 1088 388 111 558 294 211 460 470 353 71 241 2999 199 188 353 117 -1
4 141 359 247 940 882 494 306 200 100 300 190 140 880 200 223 188 435 940 241 223 -1
5 140 160 20 20 223 60 20 95 360 70 220 400 217 58 235 1880 200 175 85 -1
;
Sample SAS Program B7(Continued)
proc format;
value pp 1='Pre-1963'
2='1963-1967'
3='1968-1969'
4='1970-1971'
5='1972-1974';
run;
proc sgplot data=emissions;
title "Hydrocarbon Emissions Distribution by Period";
vbox hc / category=period datalabel;
format period pp.;
run;
Download