Answers to SAS Certification Assignments
From BingWiki
Contents |
[edit]
Section 1
Exercise 1:input
Exercise 2:infile, input
Exercise 3:
data students; infile 'C:\Documents and Settings\Yuan-Ting Wang\UserData\cat.txt' (obs=10); run;
or
data students; set cats (obs=10); run;
Exercise 4: once
Exercise 5: missover and truncover
Exercise 6: a.Change (firstobs=2) to (obs=3) b.Change (firstobs=2) to (firstobs=2 obs=3)
[edit]
Section 2
[edit]
example 1
data sealife; input name $ family $ length ; datalines; beluga whale 15 whale shark 40 basking shark 30 gray whale 50 mako shark 12 sperm whale 60 dwarf shark .5 whale shark 40 humpback . 50 blue whale 100 killer whale 30 ; run; data newsealife; set sealife; newlength=length; format newlength 10.2; run; proc print data=newsealife; var newlength; run;
[edit]
example 2
data candy; input Name $ ClassRm Month Day Year Candy $ Quantity ; cards; Adriana 21 3 2 2000 MP 7 Nathan 14 2 28 2000 CD 19 Matthew 14 3 1 2000 CD 14 Claire 14 3 3 2000 CD 11 Caitlin 21 2 24 2000 CD 9 Ian 21 3 3 2000 MP 18 Chris 14 2 18 2000 CD 6 Anthony 21 6 1 2000 MP 13 Stephen 14 3 25 2000 CD 10 Erika 21 3 25 2000 MP 17 ; run; proc means data=candy min max; var quantity; run;
[edit]
example 3
data candy; input Name $ ClassRm Month Day Year Candy $ Quantity ; cards; Adriana 21 3 2 2000 MP 7 Nathan 14 2 28 2000 CD 19 Matthew 14 3 1 2000 CD 14 Claire 14 3 3 2000 CD 11 Caitlin 21 2 24 2000 CD 9 Ian 21 3 3 2000 MP 18 Chris 14 2 18 2000 CD 6 Anthony 21 6 1 2000 MP 13 Stephen 14 3 25 2000 CD 10 Erika 21 3 25 2000 MP 17 ; run; data candy1; set candy; /*if (quantity<12) then quantity=1; else quantity=2;*/ /* Here's another way to group*/ group=1; if (quantity>=12) then group=2; run; proc print data=candy1; var group; run;
[edit]
example 4
data candy; input Name $ ClassRm Month Day Year Candy $ Quantity ; cards; Adriana 21 3 2 2000 MP 7 Nathan 14 2 28 2000 CD 19 Matthew 14 3 1 2000 CD 14 Claire 14 3 3 2000 CD 11 Caitlin 21 2 24 2000 CD 9 Ian 21 3 3 2000 MP 18 Chris 14 2 18 2000 CD 6 Anthony 21 6 1 2000 MP 13 Stephen 14 3 25 2000 CD 10 Erika 21 3 25 2000 MP 17 ; run; data candy1; set candy; /*if (quantity<12) then quantity=1; else quantity=2;*/ /* Here's another way to group*/ group=1; if (quantity>=12) then group=2; run; proc print data=candy1; var group; run; proc sort data=candy1 out=candy2; by group; run; proc print data=candy2; run; proc means data=candy2 mean; var quantity; by group; run;
[edit]
example 5
data candy; input Name $ ClassRm Month Day Year Candy $ Quantity ; cards; Adriana 21 3 2 2000 MP 7 Nathan 14 2 28 2000 CD 19 Matthew 14 3 1 2000 CD 14 Claire 14 3 3 2000 CD 11 Caitlin 21 2 24 2000 CD 9 Ian 21 3 3 2000 MP 18 Chris 14 2 18 2000 CD 6 Anthony 21 6 1 2000 MP 13 Stephen 14 3 25 2000 CD 10 Erika 21 3 25 2000 MP 17 ; run; data candy1; set candy; /*if (quantity<12) then quantity=1; else quantity=2;*/ /* Here's another way to group*/ group=1; if (quantity>=12) then group=2; run; proc print data=candy1; var group; run; proc sort data=candy1 out=candy2; by group; run; proc print data=candy2; run; /*proc means data=candy2 mean; var quantity; by group; run;*/ proc means data=candy2 mean; var quantity; class group; run;
[edit]
Session 3
[edit]
Exercise 3.1
data candy; input Name $ ClassRm Month Day Year Candy $ Quantity ; cards; Adriana 21 3 2 2000 MP 7 Nathan 14 2 28 2000 CD 19 Matthew 14 3 1 2000 CD 14 Claire 14 3 3 2000 CD 11 Caitlin 21 2 24 2000 CD 9 Ian 21 3 3 2000 MP 18 Chris 14 2 18 2000 CD 6 Anthony 21 6 1 2000 MP 13 Stephen 14 3 25 2000 CD 10 Erika 21 3 25 2000 MP 17 ; run; ods html file='c:\temp\corr.html'; title 'Candy data'; proc corr data=candy; var classrm quantity; run; ods html close;
[edit]
Exercise 3.2
data funds; input name $ return 44-47 SDAR ; cards; Group Securities. Common Stock Fund 15.1 19.1 Incorporated Investors 14.0 25.5 Investment Company of America 17.4 21.8 Investors Mutual 11.3 12.5 Loomis-Sales Mutual Fund 10.0 10.4 Massachusetts Investors Trust 16.2 20.8 National Investors Corporation 18.3 19.9 National Securities-Income Series 12.4 17.8 New England Fund 10.4 10.2 Massachusetts Investors-Growth Stock 18.6 22.7 Group Securities. Fully Administered Fund 11.4 14.1 ; run; proc print data=funds; run; data funds1; set funds; first5=substr(name,); run; proc print data=funds1; run; proc sort data=funds1; by name; run; proc print data=funds1; run;
[edit]
Exercise 3.3
data hw; input time days; cards; 100 3 150 4 99 5 . 6 160 3 80 . 200 3 ; run; data hw1; set hw; avehw=mean(time/days); run; proc print data=hw1; run;
[edit]
Exercise 3.4
data salary;
input FIELD$1-35 WOMEN MEN;
cards;
Business, Finance, Etc. 9.3 13.0
Labor Economics 10.3 12.0
Monetary-Fiscal 8.0 11.6
General Economic Theory 8.7 10.8
Population, Welfare Programs, Etc. 12.0 11.5
Economic Systems and Development 9.0 12.2
;
run;
proc print data=salary;
run;
data salary1;
set salary;
array T {2} women men;
do i=1 to 2;
T[i]=T[i]*1000;
end;
run;
proc print data=salary1;
run;
[edit]
Section 4
[edit]
4.1 and 4.2
data fitness;
input Age Weight Oxygen RunTime RestPulse RunPulse MaxPulse @@;
datalines;
44 89.47 44.609 11.37 62 178 182 40 75.07 45.313 10.07 62 185 185
44 85.84 54.297 8.65 45 156 168 42 68.15 59.571 8.17 40 166 172
38 89.02 49.874 9.22 55 178 180 47 77.45 44.811 11.63 58 176 176
40 75.98 45.681 11.95 70 176 180 43 81.19 49.091 10.85 64 162 170
44 81.42 39.442 13.08 63 174 176 38 81.87 60.055 8.63 48 170 186
44 73.03 50.541 10.13 45 168 168 45 87.66 37.388 14.03 56 186 192
45 66.45 44.754 11.12 51 176 176 47 79.15 47.273 10.60 47 162 164
54 83.12 51.855 10.33 50 166 170 49 81.42 49.156 8.95 44 180 185
51 69.63 40.836 10.95 57 168 172 51 77.91 46.672 10.00 48 162 168
48 91.63 46.774 10.25 48 162 164 49 73.37 50.388 10.08 67 168 168
57 73.37 39.407 12.63 58 174 176 54 79.38 46.080 11.17 62 156 165
52 76.32 45.441 9.63 48 164 166 50 70.87 54.625 8.92 48 146 155
51 67.25 45.118 11.08 48 172 172 54 91.63 39.203 12.88 44 168 172
51 73.71 45.790 10.47 59 186 188 57 59.08 50.545 9.93 49 148 155
49 76.32 48.673 9.40 56 186 188 48 61.24 47.920 11.50 52 170 176
52 82.78 47.467 10.50 53 170 172
;
run;
proc reg data=fitness outest=result1 noprint;
model age=Restpulse weight;
output out=fitness1 r=resid;
run;
proc print data=fitness1;
run;
data fitness2;
set fitness1;
newvar=abs(resid) ;
run;
proc sort data=fitness2;
by descending newvar;
run;
proc print data=fitness2;
run;
data fitness3;
set fitness2;
if newvar>8 then delete;
run;
proc print data=fitness3;
run;
proc reg data=fitness3 outest=result2 noprint;
model age=Restpulse weight;
run;
proc print data=result1;
run;
proc print data=result2;
run;
proc plot data=fitness3;
plot age*weight;
run;
proc plot data=fitness;
plot age*weight;
run;
[edit]
4.3
data speeding; input x; datalines; 2 3 2 1 3 6 8 9 10 13 2 12 ; run; data month; month=1; output; do i=1 to 11; month=month+1; output; end; run; data fullinfo; merge month speeding; run; proc print data=fullinfo; run;
[edit]
Section 5
[edit]
example 1
data phone; input City $11. @12 State $ Zip $ Phonenum $; cards; cary NC 27513 6224549 cary NC 27513 6223251 chapel-hill NC 27514 9974794 raleigh NC 27612 6970450 raleigh NC 27612 6791125 cary NC 27513 6224550 ; run; proc print data=phone; run; data phone1; set phone; areacode=substr(phonenum,1,3); len=length(city); run; proc print data=phone1; run;
[edit]
example 2
data GDP; input year $ y x; cards; 1982 100.0000 100.0000 1983 100.5000 102.0000 1984 100.4000 104.6000 1985 101.3000 106.1000 1986 104.4000 108.3000 1987 104.3000 109.4000 1988 104.4000 110.4000 1989 103.0000 109.5000 1990 103.2000 109.7000 1991 103.9000 110.1000 ; run; proc print data=gdp; run; proc reg data=gdp; model y=x; run;
[edit]
example 3
data GDP2; input year $ @11 y 3.2 @23 x 3.2; cards; 1982 100.0000 100.0000 1983 100.5000 102.0000 1984 100.4000 104.6000 1985 101.3000 106.1000 1986 104.4000 108.3000 1987 104.3000 109.4000 1988 104.4000 110.4000 1989 103.0000 109.5000 1990 103.2000 109.7000 1991 103.9000 110.1000 ; run; proc print data=gdp2; run; proc reg data=gdp2; model y=x; run;
Another way to solve the problem:
data GDP; input year $ y x ; cards; 1982 100.0000 100.0000 1983 100.5000 102.0000 1984 100.4000 104.6000 1985 101.3000 106.1000 1986 104.4000 108.3000 1987 104.3000 109.4000 1988 104.4000 110.4000 1989 103.0000 109.5000 1990 103.2000 109.7000 1991 103.9000 110.1000 ; run; proc print data=gdp; run; proc reg data=gdp; model y=x; run; data gdp1; set gdp; newy=(y/100); newx=(x/100); run; proc print data=gdp1; run; proc reg data=gdp1; model newy=newx; run;
[edit]
example 4
data regression; input y x2 $ x3; cards; 10 1 1 8 2 3 6 3 5 4 4 7 2 5 9 0 6 11 2 7 13 ; run; proc print data=regression; run; proc reg data=regression; model y=x3; run; data new; set regression; ly=log(y); lx3=log(x3); run; proc print data=new; run; proc reg data=new; model ly=lx3; run;
[edit]
example 5
proc print data=ny(obs=20); var COUNTY_NAME F_FUNDS T_FUNDS ; run;
[edit]
Section 6
data test; set sashelp.air; format date juldate.; run; proc print data=test(obs=10); run; data _null_; set sashelp.air (obs=50); file 'c:\Temp\air50.txt'; put @1 date date7. @9 air 4. ; run; data test1; infile 'c:\Temp\air50.txt'; input date date9. air; run; proc print data=test1(obs=10); run;
[edit]
Session 7
[edit]
exercise 1
data airt; set sashelp.air; day=day(date); mo=month(date); yr=year(date); decade=(int(yr/10)-190)*10; run; proc print data=airt; run; data airt; set airt; drop day date; run; proc print data=airt; run; proc means data=airt; by decade; var air; output out=maxair max=mair; run; proc print data=maxair; run; data test; set maxair; drop _type_ _freq_; run; data 'c:\temp\airmax.sas7bdat'; set test; run;
data airmax; set 'c:\temp\airmax.sas7bdat'; run; proc sort data=airmax; by decade; run; proc print data=airmax; run; proc sort data=airt; by decade; run; proc print data=airt; run; data final; merge airt airmax; by decade; relative=((mair-air)/mair)*100; run; proc print data=final; run;
[edit]
exercise 2
First you have to save the data(1992-2002) in your computer, and then try to import it into SAS. Then do the following steps:
data employment; set employment1; run; data employment2; input Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec; cards; 2003 74.4 74.4 74.2 74.2 74.1 74.0 74.0 73.8 73.9 73.7 73.3 73.3 2004 72.7 73.2 72.8 72.9 73.1 73.1 73.1 73.2 73.0 72.9 72.7 72.4 ; run; data combine; set employment employment2; run; proc print data=combine; run;
[edit]
exercise 3
Use 'nodupkeys' which introduced in the workshop last week.
[edit]
Session 8
[edit]
exercise 1
/*data base; array inning (*) var2-var10; set baseball; runs=0; do i = 1 to 9; runs=runs+ inning(i); end; rename var1=team; keep team runs; run; */ * CLAMCHOWFF VEGMED; data soup2; set soup; array flavor (*) var3-var59; rename var1=street var2=city; ff=0; vm=0; do i=1 to 57; if (flavor(i)='CLAMCHOWFF') then FF=1; if (flavor(i)='VEGMED') then VM=1; end; drop i; run; data soup3; set soup2; array flavor (*) var3-var59; nchick=0; do i=1 to 57; if (substr(flavor(i) ,1,5)='CHICK') then nchick=nchick+1; end; run; data soup4; set soup; array flavor (*) var3-var59; newchick=0; do i=1 to 57; if (index(flavor(i),'CHICK')>0) then newchick=newchick+1; end;

