0% found this document useful (0 votes)
24 views10 pages

SAS Programming Practice Scenarios

The document outlines various programming scenarios for practicing SAS programming, including tasks such as sorting datasets, merging data, and creating new variables. Each scenario provides directions, testing questions, and objectives to be accomplished. The scenarios cover a range of data manipulation techniques and reporting methods using SAS.

Uploaded by

lucia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

SAS Programming Practice Scenarios

The document outlines various programming scenarios for practicing SAS programming, including tasks such as sorting datasets, merging data, and creating new variables. Each scenario provides directions, testing questions, and objectives to be accomplished. The scenarios cover a range of data manipulation techniques and reporting methods using SAS.

Uploaded by

lucia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

365

Chapter 17

Practice Programming
Scenarios

Scenario 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Scenario 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Scenario 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Scenario 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Scenario 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Scenario 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Scenario 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Scenario 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Scenario 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
366 Chapter 17 • Practice Programming Scenarios

Scenario 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Test Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Objectives Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

Scenario 1

Directions
This scenario uses the [Link] and [Link] data sets. Write a SAS program to
do the following:
• Sort [Link] and [Link] by ID.
• Use the match-merging technique to combine [Link] and [Link] to
create a new temporary data set named [Link].
• Select observations for the patients under the age of 50.
• Sort the new data set, [Link], by Age in descending order.
• Save the sorted data set as [Link].

Test Your Code


1. What the value of the Age variable for observation 6 in [Link]?
2. What is the value of the Weight variable for observation 3 in [Link]?

Objectives Accomplished
• Combine SAS data sets using match-merging.
• Use a DATA step to create a SAS data set from an existing SAS data set.
• Use the IF/THEN-ELSE statements to process data conditionally.
• Use the SORT procedure to re-order observations in place or write them out to a new
data set.

Scenario 2

Directions
This scenario uses the [Link] data set. Write a SAS program to do the following:
• Create a new temporary SAS data set that uses [Link] and store the results in
Work.Stress1.
• Remove observations with RestHR values that are greater than or equal to 70.
• Create a new variable called TotalTime. The value of TotalTime is the value of
TimeMin multiplied by 60, plus the value of TimeSec.
Scenario 3 367

• Remove TotalTime values that are less than 600.

Test Your Code


1. How many observations are in Work.Stress1?
2. What is the value of TotalTime for observation 5 in Work.Stress1?

Objectives Accomplished
• Use a DATA step to create a SAS data set from an existing SAS data set.
• Control which observations and variables in a SAS data set are processed and written
out to a new data set.
• Use assignment statements in the DATA step.
• Use IF/THEN-ELSE statements to process data conditionally.

Scenario 3

Directions
This scenario uses the [Link] data set. Write a SAS program to do the following:
• Create a new temporary SAS data set that uses [Link] and store the results in
[Link].
• Select observations where WageCategory is not equal to H.
• Format the variable DOB as mmddyy10.
• Create a new variable named Raise whose value is WageRate multiplied by 3%.
• Determine the grand total of Raise for the entire data set.

Test Your Code


1. For observation 5, what is the value of DOB?
2. For observation 15, what is the value of Raise? Round your answer to 2 decimal
places.
3. What is the grand total of Raise for the entire data set?

Objectives Accomplished
• Use a DATA step to create a SAS data set from an existing SAS data set.
• Use SAS date and time formats to specify how the values are displayed.
• Control which observations and variables in a SAS data set are processed and written
out to a new data set.
• Use assignment statements in the DATA step.
368 Chapter 17 • Practice Programming Scenarios

• Use the SUM statement to accumulate subtotals and totals.

Scenario 4

Directions
This scenario uses the [Link] data set. Write a SAS program to do the following:
• Print the data set [Link] sorted and grouped by Dest. Save your sorted data
set as a temporary data set, [Link].
• Store the results, grouped by the variable Dest, in a PDF file named “LGA
[Link]” using the FestivalPrinter style with a report title, “Laguardia Flights”.

Test Your Code


1. What is the file size of the LGA [Link] file?
2. What is the value of Boarded in observation 13?
3. What is the destination value in observation 42?

Objectives Accomplished
• Enhance reports system with user-defined formats, titles, footnotes, and SAS System
reporting options.
• Generate reports using ODS statements:
• Identify the ODS destinations.
• Create HTML, PDF, and RTF files with ODS statements.
• Use the STYLE= option to specify a style template.

Scenario 5

Directions
Open the ehs01 program from the EHS folder and correct the errors in the program. For
instructions, see the commented code in the program that is marked by a slash (/) and an
asterisk (*).
Example Code 1 ehs01 Program: Fix the Errors

data [Link] drop=Total, EquipCost; /* #1 */


set [Link];
if Days > 7 then Discount=(RoomCharge)*20% else 0; /* #2 */
TotalDue=Total-Discount; /* #3 */
format DateIn DateOut date9; /* #4 */
format RoomRate RoomCharge Discount TotalDue dollar10.; /* #5 */
Scenario 6 369

proc print data=[Link]; /* #6 */

Here are instructions that are commented in the program, ehs01.


1 Drop the variables Total and EquipCost.
2 If the Days variable is greater than 7, then Discount is the value of RoomCharge
multiplied by 20 %. If the Days variable is less than or equal to 7, then Discount is
set to 0.
3 Create a new variable, TotalDue, with a value of Total minus Discount.
4 Format DateIn and DateOut to appear as 05APR2009.
5 Format the variables RoomRate, RoomCharge, Discount, and TotalDue to appear as
$100.00.
6 Print your results.

Test Your Code


1. What is the value of the variable TotalDue in observation 4?
2. What is the value of the variable Discount in observation 5?

Objectives Accomplished
• Identify the characteristics of SAS statements.
• Define SAS syntax rules and identify syntax errors such as misspelled keywords,
unmatched quotation marks, missing semicolons, and invalid options.
• Use the log to help diagnose syntax errors in a given program.

Scenario 6

Directions
This scenario uses the Excel file [Link]. Write a SAS program to do the following
and store the results in the data set [Link].
• Import the Excel file [Link].
• Drop the AgeAtDeath and DeathCause variables from the [Link] data set.
• Include only the observations where Status=Alive in the [Link] data set.
• If the AgeCHDdiag variable has a missing value (.), then do not include the value in
[Link].
• Create a new variable Smoking_Status, set its length to 17 characters, and use the
following criteria:
• If the value of Smoking is between 0 and less than 6, then Smoking_Status is
"None (0–5)".
• If the value of Smoking is between 6 and 15 inclusively, then Smoking_Status is
"Moderate (6–15)".
370 Chapter 17 • Practice Programming Scenarios

• If the value of Smoking is between 16 and 25 inclusively, then Smoking_Status


is "Heavy (16–25)".
• If the value of Smoking is greater than 25, then Smoking_Status is "Very Heavy
(>25)".
• If there are any other values for the variable Smoking, set Smoking_Status to
"Error".
• Create a two-way frequency table using variables AgeCHDdiag and Smoking_Status
and suppress column percentage, row percentage, and cell percentage.

Test Your Code


1. What is the total number of Heavy Smokers (16–25)?
2. What is the frequency value of Very Heavy (>25) smokers for AgeCHDDiag 48?
3. What is the frequency value of Smoking_Status = "Error"?

Objectives Accomplished
• Access SAS data sets with the SET statement.
• Use a DATA step to create a SAS data set from an existing SAS data set.
• Use IF-THEN/ELSE statements to process data conditionally.
• Use assignment statement to create new variables and assign a value in the DATA
step.
• Define the length of a variable using the LENGTH statement.
• Generate summary reports and frequency tables using Base SAS procedures.

Scenario 7

Directions
This scenario uses the Cert.Temp18 data set. Write a SAS program to do the following:
• Create a temporary SAS data set that uses Cert.Temp18 and store the results as
Work.Scenario7.
• Format the Day variable so that the date appears as 01JAN2018.
• Use a function to create a variable named Month that is equal to the numeric value of
the month of the Day variable. For example, if the month is January, Month=1, if the
month is February, Month=2, and so on.
• Create a one-way frequency table using the variable HighTemp.
• Use PROC MEANS to calculate the mean and standard deviation for the variables
AvgHighTemp and AvgLowTemp by the new Month variable.
Scenario 8 371

Test Your Code


1. What is the frequency for a HighTemp of 63?
2. What is the HighTemp on January 12, 2018?
3. What is the mean for AvgLowTemp for Month=1? (Round your answer to the
nearest integer.)
4. What is the standard deviation (std) for AvgHighTemp for Month=3? (Round your
answer to two decimal places.)

Objectives Accomplished
• Use a DATA step to create a SAS data set from an existing SAS data set.
• Use SAS date and time formats to specify how the values are displayed.
• Use assignment statements to create new variables and assign a value in the DATA
step.
• Use SAS functions to manipulate character data, numeric data, and SAS date values.
• Generate summary reports and frequency tables using Base SAS procedures.

Scenario 8

Directions
This scenario uses the [Link] data set. Write a SAS program to do the
following:
• Create a temporary SAS data set that uses [Link] and store the results in
Work.Scenario8.
• Extract the 5-digit ZIP codes from the State variable and store them in the ZipCode
variable.
• Extract the two letters from the State variable and store them in the State variable.
• Create a one-way frequency table using the variable State.

Test Your Code


1. What is the frequency for the state of NY?
2. Which observation contains ZipCode 85069?
3. How many states have the frequency number of 4?

Objectives Accomplished
• Use a DATA step to create a SAS data set from an existing SAS data set.
372 Chapter 17 • Practice Programming Scenarios

• Use assignment statements to create new variables and assign a value in the DATA
step.
• Use SAS functions such as SUBSTR to search a character value and extract a portion
of the value.
• Generate frequency tables using Base SAS procedures.

Scenario 9

Directions
This scenario uses the [Link], [Link], and Cert.Empdatu2 data sets. Write
a SAS program to do the following:
• Concatenate [Link], [Link], and Cert.Empdatu2 to create
[Link].
• Create a macro variable named &Location and set the value of this macro variable to
USA.
• Include only the observations whose value for Country is the value of the macro
variable.
• Keep only observations whose salary is $30,000 or greater.
• Sort the data by variable Salary in descending order.
• Use PROC EXPORT to export the data to a CSV file and save it as
[Link].

Test Your Code


1. What is the value of Salary in observation 4?
2. What is the size (in bytes) of the CSV file that you exported?

Objectives Accomplished
• Use a DATA step to create a SAS data set from an existing SAS data set.
• Create a simple raw data file by using the EXPORT procedure as an alternative to the
DATA step.
• Use the WHERE statement in the DATA step to select observations to be processed.
• Use the SORT procedure to re-order observations in place or write them out to a new
data set.
• Create macro variables with the %LET statement.
Scenario 10 373

Scenario 10

Directions
Open the ehs02 program from the EHS folder and correct the errors in the program
below.
Example Code 2 ehs02 Program: Fix the Errors

[Link];
set [Link];
AvgMPG=mean(mpg_city, mpg_highway);
run;
title 'Cars With Average MPG Over 40';
proc print data=[Link]
var make model type avgmpg;
where AvgMPG>40;
run;
title 'Average MPG by Car Type';
proc means data=[Link] avg min max maxdec=1;
var avgmpg;
class type;
run;
title;

Test Your Code


1. What is the number of observations where the variable Type is Sedan?
2. How many observations are printed to the report titled, “Cars With Average MPG
Over 40”?

Objectives Accomplished
• Identify the characteristics of SAS statements.
• Define SAS syntax rules and identify common syntax errors such as misspelled
keywords, unmatched quotation marks, missing semicolons, and invalid options.
• Use the log to help diagnose syntax errors in a given program.
374 Chapter 17 • Practice Programming Scenarios

Common questions

Powered by AI

To summarize and visualize data using SAS, leverage BASE SAS procedures and functions. Utilize summarization procedures, such as generating frequency tables and means, by extracting data elements using functions like SUBSTR for character manipulation . Follow up with visualization processes like PROC CHART or generating styled reports using ODS, which transform raw data into understandable formats and visual representations .

Create a new SAS data set using a DATA step from an existing data set. Control which observations and variables are processed using IF/THEN-ELSE statements to process data conditionally. For example, remove observations with certain values, such as RestHR values greater than 70, and create new variables using assignment statements, like calculating TotalTime from TimeMin and TimeSec . Format data appropriately by using formats like mmddyy10 for dates .

To import data from Excel in SAS, use PROC IMPORT to load data into a SAS dataset. For example, import from heart.xlsx into Work.Heart . After import, manipulate the dataset by using a DATA step to drop unnecessary columns, such as AgeAtDeath, and sort records using the SORT procedure on specific variables like Salary. This allows efficient data handling and preparation for further analysis .

Specifying SAS syntax accurately is crucial in reducing errors and ensuring that complex datasets are created correctly. Achieving this involves defining syntax rules and using the SAS log to identify errors, such as unmatched quotation marks or invalid options. By addressing these issues, data manipulation processes become reliable, allowing correct dataset creation . Techniques include using WHERE statements to filter data and SORT procedures for ordering, thereby increasing data integrity .

Handling errors in SAS involves identifying and correcting errors by examining the log for syntax issues like misspelled keywords or missing semicolons . Improve code accuracy by following directions in comments, such as dropping irrelevant variables or correctly calculating values like Discounts. Proper formatting of outputs through statements like format DateIn as date9 ensures clarity. Using these practices not only resolves errors but also structures code efficiently .

Use conditional statements like IF/ELSE within a DATA step to perform financial calculations in SAS, such as calculating Discount based on RoomCharge when Days exceed a certain threshold . Once calculated, format monetary output using statements like format Discount and TotalDue as dollar10. to display as currency. These techniques ensure accurate financial reporting and user-friendly data presentation .

To generate grouped and formatted reports in SAS, use the ODS (Output Delivery System) along with sorting the data by relevant variables such as Dest . Utilize styles, like FestivalPrinter, and create report titles for better readability. Save data as PDF files to facilitate sharing, and use ODS statements for various formats like HTML and PDF. Best practices include defining user-specific formats, titles, and grouping data effectively for improved clarity in reports .

Create a concatenated dataset by using a DATA step or a PROC SQL operation to append multiple datasets, such as Cert.Empdata, Cert.Empdatu, and Cert.Empdatu2, into one SAS dataset, e.g., Work.FlightEmpData . Use conditional selections like WHERE statements to filter. For exporting, the PROC EXPORT procedure writes the dataset to a format like CSV, storing it as flightempdata.csv . These steps consolidate data sources for streamlined analysis and output .

Macro variables in SAS streamline data processing by storing values for repeated use, thereby ensuring uniformity across data manipulation tasks, such as filtering datasets. The use of macro variables, like &Location with a value of USA, allows for conditionally selecting observations, such as keeping only those with Country matching the macro variable . This enables dynamic and efficient data management, significantly enhancing the workflow .

To merge and sort multiple SAS datasets, use the match-merging technique to combine datasets, such as Cert.Patients and Cert.Measure . After merging, select the specific observations based on a condition, such as patients under the age of 50. Next, use the SORT procedure to sort the new data set in descending order by a specific variable like Age, then save it as a new sorted dataset .

You might also like