CS代写 SAS BASE PROGRAMMING

SAS BASE PROGRAMMING
– Lecture 12 –

Objectives

Copyright By PowCoder代写 加微信 powcoder

 Do Loops
 Write to External Files
 Create custom reports
 Reading Non-standard Data Files  Delimiters other than spaces
 Mixed format records
 Multiple records per line
 SAS Variable Lists
 Data Conversion
 Numeric to character  Character to numeric

DO Loop Processing
 There are four kinds of DO loops in SAS:  DO-END
 Executes statements as a unit, usually as a part of IF-THEN/ELSE statements.
 Iterative DO
 Executes a group of statements repetitively based on the value of an
index variable.
 DO WHILE
 Executes a group of statements repetitively as long as the specified condition remains true. The condition is checked before each iteration of the loop.
 DO UNTIL
 Executes a group of statements repetitively until the specified condition is true. The condition is checked after each iteration of the loop.

 For one executable statement, the IF-THEN statement does NOT require a DO-END statement
 For more than one executable statement, the IF- THEN statement requires a DO-END statement
IF sex = ‘Male’ THEN Abbreviation = ‘Mr. ‘;
IF sex = ‘Male’ THEN DO;
Abbreviation = ‘Mr. ‘;
Salutation = ‘Sir ‘;

Iterative DO
 Use the iterative DO to repeat a group of statements a fixed number of times, based on the value of an index variable
 It helps us to perform repetitive calculations

Perform Repetitive Calculations
 On January 1 of each year (2021-2023), $5,000 is invested in an account. Determine the value of the account after three years based on a constant annual interest rate of 7.5 percent.
data invest;
do Year=2021 to 2023;
Capital+5000;
Capital+(Capital*.075); end;
Year Capital
2024 17364.61

Perform Repetitive Calculations
 Now display the value of the account at the end of each year
data invest;
do Year=2021 to 2023;
Capital+5000; Capital+(Capital*.075); output;
Year Capital 2021 5375.00 2022 11153.13 2023 17364.61

Perform Repetitive Calculations
 Create a program that shows, step by step  The summation of 1 to 10
 The calculation of 10! (10 factorial)
data iterate; Summation = 0; Factorial = 1; n = 10;
do i = 1 to n;
Summation = Summation + i; Factorial = Factorial * i; output;

Example Output
Obs i Summation Factorial
3366 4410 24 5 5 15 120
55 3628800

Forecasting Example
 In our last lecture, we created a data set containing weight projections for individuals.
 Let’s look at a similar problem.
 Use the growth data set in the data2 folder
 Forecast the number of employees forward for the next three years, assuming an annual increase of 10%
 Compare the old method vs. the new method

Forecasting Example
data forecast;
set data2.growth(rename=(NumEmps=NewTotal)); Increase = .1;
NewTotal=NewTotal*(1+Increase);
NewTotal=NewTotal*(1+Increase);
NewTotal=NewTotal*(1+Increase);
data forecast;
set data2.growth(rename=(NumEmps=NewTotal)); Increase = .1;
do Year=1 to 3;
NewTotal=NewTotal*(1+Increase);
output; end;

More on the Iterative DO Statement
 The index variable can start or stop on any number  The index variable can iterate in multiples using the
do i = 5 to 20;
do i = 1 to 100 by 2;
output; end;
 Index variables can take on user-defined lists
do Day = ‘Mon’,’Tue’,’Wed’,’Thu’,’Fri’,’Sat’,’Sun’; output;

The Iterative DO Statement
 How many times will each DO loop execute?

DO-WHILE and DO-UNTIL Loops
 A DO-WHILE Loop executes a group of statements repetitively as long as the specified condition remains true. The condition is checked before each iteration of the loop.
 A DO-UNTIL Loop executes a group of statements repetitively until the specified condition is true. The condition is checked after each iteration of the loop.

Conditional Iterative Processing
 Determine the number of years it would take for an account to exceed $1,000,000 if $5,000 is invested annually at 7.5 percent interest.
DATA invest;
do while(Capital<1000000); Capital+5000; Capital+(Capital*.075); Iterative DO with Conditional Clause  Determine the return of the account again. Stop the loop if 25 years is reached or more than $250,000 is accumulated More About the Data Step  Note: INFILE vs. FILE, INFORMAT vs. FORMAT, INPUT vs. PUT  One controls data coming into the DATA step, the other controls data leaving the DATA step Create Custom Reports  You can use the PUT statement with the DATA _NULL_ statement to write output in any specified  DATA _NULL_ begins a DATA step but does NOT create a data set.  It writes output to a specified file using PUT statements  The PUT statements control the exact location and format of information in the output file DATA _NULL_ Program and Output data _null_; set data1.admit; if Sex = 'M' then Salutation = 'Mr.'; Else Salutation = 'Ms.'; file '/home/user/admitreport.doc' PRINT; put @5 'Dear ' Salutation ' ' Name ':' // @5 'Your weight at admission was ' Weight 3. ' pounds.' / @5 'Your height at admission was ' Height 2. ' inches.' // @10 'Thank you for your ' fee dollar8.2 ' payment!'; put _page_; Dear Mr. Murray, W : Your weight at admission was 168 pounds. Your height at admission was 72 inches. Thank you for your $85.20 payment! Colon Modifier  The Colon Modifier is used to read values only as far as the next delimiter  Allows for the use of Informats with List input to handle non- standard data values (i.e., values that do not match the format) Colon Modifier: Character Example  In List input, use the Colon Modifier to read character values longer than 8 characters (with no blank spaces). The ‘:’ tells SAS to read in a value until it reaches a space: data students; input Name : $20. Gender : $6. Age 2.; datalines; Elizabeth female 23 Jonathan male 22 Zack male 19 INFILE Statement Options Using Other Delimiters  A delimiter is used to separate values of adjacent variables  In SAS, the default delimiter is a “space,” but many other programs use commas or tabs Space delimited vs. Comma delimited Betty 19 F Betty,19,F The DLM= Option  The DLM= option is used to specify a delimiter  The delimiter can be any character  Example: infile 'students.txt' dlm=','; input Name $ Age Gender $; Mary,21,Female John,22,Male David,18,Male Missing Data at the End of a Row  By default, when there is missing data at the end of a row 1. SAS loads the next record to finish the observation 2. A note is written to the log 3. SAS loads a new record at the top of the DATA step and continues processing Missing Data at the End of a Row Missing Data at the End of a Row Solution: MISSOVER Option  The MISSOVER option tells SAS it should not read from the next line if there are any missing values data airplanes3; infile 'airdata2.txt' dlm=',' missover; input ID $ InService : date9. PassCap CargoCap; ID Service 50001 10627 50002 10907 50003 11617 50004 12088 50005 12228 50006 12772 90 530 172 . 170 510 180 520 In Pass Carg The DSD Option  The DSD option:  Sets the default delimiter to a comma  Treats consecutive delimiters as missing values  Enables SAS to read values with embedded delimiters if the value is surrounded by double quotes Mixed Record Types  What if not all records have the same format? Desired Output The Trailing @ Modifier  The trailing @ modifier tells SAS to hold the current input buffer line for further processing: data sales; infile '/home/user/sales.dat'; input SalesID $ Location $ @; if location='USA' then input SaleDate : mmddyy10. else if Location='EUR' then input SaleDate : date9. Amount : commax8.; Mixed Record Types Multiple Observations Per Line The Double Trailing @ Modifier Trailing @ vs. Double Trailing @ SAS Variable Lists  Numbered range lists  Name range lists  Name prefix lists  Special SAS name lists Numbered Range List  Allows you to refer to a set variables that all start with the same variable name, and all end with a number  Example:  A numbered range list refers to all the weeks as follows: input Salesman $ Week1 Week2 Week3 Week4...Week52; PROC print; var Week1-Week52; Name Range List  Allows you to refer to a series of variables that appear in consecutive order in the data set  Example:  A name range list refers to all the days as follows: Note: Salesman-Numeric-Region includes only Numeric variables in the range, and Salesman-Character-Region includes only character variables in the range input Salesman $ Mon Tues Wed Thurs Fri Sat Sun Region; PROC print; var Mon--Sun; Name Prefix List  Some SAS functions and statements allow you to refer to all variables that begin with a specified character string.  Example:  This calculates the sum of all the variables that begin with 'SALES', such as SALES_JAN, SALES_FEB, and SALES_MAR. sum(of SALES:) Special SAS Names List  Special SAS name lists include  _NUMERIC_  Specifies all numeric variables that are already defined in the current DATA step.  _CHARACTER_  Specifies all character variables that are already defined in the current DATA step.  Specifies all variables that are already defined in the current DATA step. Data Conversion  In some instances, you may need to convert one data type into another.  You can convert data types  Implicitly by allowing the SAS System to do it for you  Explicitly with these functions:  INPUT character-to-numeric conversion  PUT numeric-to-character conversion. Automatic Character-to-Numeric Conversion  The data2.salary1 data set contains a character variable GrossPay. Compute a 10 percent bonus for each employee.  What will happen when the character values of GrossPay are used in an arithmetic expression? Automatic Character-to-Numeric Conversion  SAS automatically converts a character value to a numeric value when the character value is used in a numeric context, such as  Assignment to a numeric variable  An arithmetic operation  Logical comparison with a numeric value  A function that takes numeric arguments The INPUT Function  The INPUT function uses an informat to convert a character string into a number NumVar = INPUT(source,informat); Character-to-Numeric Conversion  The data2.salary2 data set also contains a character variable GrossPay. Now use this data set to compute a 10 percent bonus for each employee.  What will happen when the character values of GrossPay are used in an arithmetic expression? Failed Character-to-Numeric Conversion  This approach will not work because GrossPay has non-numeric characters (commas). Explicit Character-to-Numeric Conversion Automatic Numeric-to-Character Conversion  SAS automatically converts a numeric value to a character value when the numeric value is used in a character context, such as  assignment to a character variable  a concatenation operation  a function that accepts character arguments The PUT Function  The PUT function uses a SAS format to convert a number into a character string: Birth = 12862  BirthDate = PUT(Birth,mmddyy8.) = “03/20/95” CharVar = PUT(source,format); INPUT vs. PUT Functions  These are NOT the same as the INPUT and PUT statements  Functions  The INPUT function converts character data into numeric data  The PUT function converts numeric data into character data  Statements  The INPUT statement controls how data is read into a data  The PUT statement controls how data is written out of a data step 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com