SAS Assignment 1 5 E057
SAS Assignment 1 5 E057
SAS Assignment 1 5 E057
Activity 1.03 *;
* 1) View the code. How many steps are in the program? => 3
* 1) Format the program to improve the spacing. What syntax error is detected? Fix the error
and run the program.
data canadashoes;
set sashelp.shoes;
where region="Canada";
Profit=Sales-Returns;
run;
proc print
data=canadashoes; run;
*; LOG:
RESULTS:
* code. =>
proc sort data=pg1.storm_summary;
by Season Name;
run;
* Activity 2.07 *;
* 1) Complete the OPTIONS statement to ensure that the column names follow SAS
naming conventions. *;
options VALIDVARNAME=V7 ;
* 2) Complete the LIBNAME statement to create a library named NP that reads NP_INFO.XLSX
in the data folder.
LIBNAME NP XLSX "/folders/myfolders/2. Datasets/PG1/data/pg194/np_info.xlsx";
* 3) Highlight the OPTIONS and LIBNAME statements and *;
NP.Parks
NP.SPECIES:
NP.VISITS
* Activity 2.08 *;
* library. *;
libname NP CLEAR;
* conventions? *;
* Activity 2.09 *;
* import is successful. *;
FIRST RUN:
SECOND RUN:
* Activity 3.02 *;
The where statements are not applied as MaxWindMPH has the value 132 which is smaller than 156
* 2) Change the second WHERE statement to WHERE ALSO *;
Modified Code:
proc print data=pg1.storm_summary;
where MaxWindMPH>156;
where also MinPressure>800 and MinPressure<920;
run;
Results:
* Activity 3.03 *;
FIRST Where
SECOND Where
THIRD Where
FOURTH Where
CODE:
proc print data=pg1.storm_summary(obs=50);
* Activity 3.04 *;
* SP. *;
* that step? *;
NA:
SP:
* Activity 3.06 *;
* report. *;
CODE:
date7.;
run;
* 2) Change the width of the DATE format to 7 and run *;
DATE WIDTH = 9
CODE:
CODE:
* MaxWindMPH? *;
* Activity 4.01 *;
data STORM_NEW;
set PG1.storm_summary;
run;
* 2) Define a library named out pointing to the output *;
* program. *;
data out.STORM_NEW;
set PG1.storm_summary;
run;
* Activity 4.03 *;
* STORM_CAT5. *;
* or after 01JAN2000. *;
data out.storm_cat5;
set pg1.storm_summary;
run;
* Activity 4.04 *;
data storm_length;
set pg1.storm_summary;
drop Hem_EW Hem_NS Lat Lon;
StormLength = EndDate -
StartDate;
run;
* Activity 4.05 *;
* speed measurements. *;
Code:
data storm_wingavg;
set pg1.storm_range;
WindAvg = mean(Wind1, Wind2, Wind3, Wind4);
WindRange = Range(Wind1, Wind2, Wind3,
Wind4);
run;
* Activity 4.06 *;
CODE:
data pacific;
set pg1.storm_summary;
run;
* Activity 4.07 *;
* statement. *;
data storm_cat;
set pg1.storm_summary;
keep Name Basin MinPressure StartDate PressureGroup;
*add ELSE keyword and remove final condition;
*if MinPressure=. then PressureGroup=.;
if MinPressure<=920 then PressureGroup=1;
else PressureGroup=0;
run;
* Activity 4.08 *;
* Basin='na'?
* program. *;
Ocean Length = 10
Basin = Uppercase
Code:
data storm_summary2;
set pg1.storm_summary;
where Basin = 'na';
length Ocean $10;
keep Basin Season Name MaxWindMPH Ocean;
Basin = upcase(Basin);
OceanCode=substr(Basin,2,1);
if OceanCode="I" then Ocean="Indian";
else if OceanCode="A" then Ocean="Atlantic";
else Ocean="Pacific";
run;
* Activity 4.09 *;
Reason for Fail: The program fails due to no matching If-then clause and as there were 2 unclosed Do
blocks.
* Activity 5.01 *;
* 1) In the program, notice that there is a TITLE *;
* statement followed by two procedures. Run the *;
* program. Where does the title appear in the *;
* output? *;
* 2) Add a TITLE2 statement above PROC MEANS to print *;
* a second line: *;
* Summary Statistics for MaxWind and MinPressure *;
Code:
title "Storm Analysis";
title "Summary Statistics for MaxWind and MinPressure";
No Title
* Activity 5.03 *;
* 1) This code creates a macro variable named oc that *;
* stores the text string Pacific. The oc macro *;
* variable is then used in the WHERE statement to *;
* subset the data. *;
* 2) Update the TITLE2 statement to use the macro *;
* variable. Run the program. *;
Code:
%let oc=Pacific;
ods noproctitle;
title 'Storm Analysis';
title2 &oc;
run;
ods proctitle;
title;
ods proctitle;
title;
* Activity 5.04 *;
* 1) Modify the LABEL statement in the DATA step to *;
* label the Invoice column as Invoice Price. *;
Code:
data cars_update;
set sashelp.cars;
keep Make Model MSRP Invoice AvgMPG;
AvgMPG=mean(MPG_Highway, MPG_City);
label MSRP="Manufacturer Suggested Retail
Price" AvgMPG="Average Miles per Gallon"
Invoice = "Invoice Price";
run;
* Activity 5.05 *;
* 1) Create an output table named STORM_COUNT by *;
* completing the OUT= option in the TABLES *;
* statement. *;
Code:
* 2) Run the program. Which data values are included *;
* in the output table? Which statistics are *;
* included? *;
* 3) Put StartDate and BasinName in separate TABLES *;
* statements. Add the OUT= option in each *;
* statement, and name the tables MONTH_COUNT and *;
* BASIN_COUNT. *;
Code:
title "Frequency Report for Basin and Storm Month";
proc freq data=pg1.storm_final order=freq noprint;
tables BasinName / out=BASIN_COUNT;
tables StartDate / out=MONTH_COUNT;
format StartDate monname. ;
run;
***********************************************************;
* Activity 5.07 *;
* 1) Run the PROC MEANS step and compare the report *;
* and the wind_stats table. Are the same statistics *;
* in the report and table? What do the first five *;
* rows in the table represent? *;
ANS: The first five rows represent where type =
0 CODE:
proc means data=pg1.storm_final mean median max;
var MaxWindMPH;
class BasinName;
*ways 1;
output out=wind_stats;
run;
* 2) Uncomment the WAYS statement. Delete the *;
* statistics listed in the PROC MEANS statement and *;
* add the NOPRINT option. Run the program. Notice *;
* that a report is not generated and the first five *;
* rows from the previous table are excluded. *;
ANS:
CODE:
proc means data=pg1.storm_final noprint;
var MaxWindMPH;
class BasinName;
ways 1;
output out=wind_stats;
run;
* 3) Add the following options in the OUTPUT statement *;
* and run the program again. How many rows are in *;
* the output table? *;
* output out=wind_stats mean=AvgWind max=MaxWind; *;
ANS:
CODE:
proc means data=pg1.storm_final noprint;
var MaxWindMPH;
class BasinName;
ways 1;
output out=wind_stats mean=AvgWind max=MaxWind;
run;
**************************************************;
* Activity 5.08 *;
* Run the program and examine the results to *;
* see examples of other procedures that *;
* analyze and report on the data. *;
**************************************************;
ANS: