Posted by : Netbloggy Friday, September 18, 2015

The primary objective of computer is to compute mathematical computations of big scale faster than human beings. We humans can write down multiplication table of number 5 till 10th level or  maybe 100th level, but we can make computer write down the same till 1000th or more too. And Iterative processing (looping) opens doors for such computing.

Problem 1: 

Using this (Vital) data set, create a new data set (NewVitals) with the following new
variables:
For subjects less than 50 years of age:
If Pulse is less than 70, set PulseGroup equal to Low;
otherwise, set PulseGroup equal to High.
If SBP is less than 130, set SBPGroup equal to Low;
otherwise, set SBPGroup equal to High.
For subjects greater than or equal to 50 years of age:
If Pulse is less than 74, set PulseGroup equal to Low;
otherwise, set PulseGroup equal to High.
If SBP is less than 140, set SBPGroup equal to Low;
otherwise, set SBPGroup equal to High.


libname A15001 '/folders/myfolders/iSAS/Assignment';
 
data A15001.A01_vitals;
 input ID : $3.
 Age
 Pulse
 SBP
 DBP;
 label SBP = "Systolic Blood Pressure"
 DBP = "Diastolic Blood Pressure";
datalines;
001 23 68 120 80
002 55 72 188 96
003 78 82 200 100
004 18 58 110 70
005 43 52 120 82
006 37 74 150 98
007 . 82 140 100
;
 
Solution:


 
data A15001.A01_new_vitals; 
 set A15001.A01_vitals;
 if missing(age) then delete;
 else if age lt 50 then do;
  if pulse lt 70 then PulseGroup = 'Low';
  else PulseGroup = 'High';
  if SBP lt 130 then SBPGroup = 'Low';
  else SBPGroup = 'High'; 
 end; 
 else if age ge 50 then do;
  if pulse lt 74 then PulseGroup ='Low';
  else PulseGroup = 'High';
  if SBP lt 140 then SBPGroup= 'Low';
  else SBPGroup = 'High';
 end; 
run;
 
Title "Listing of New Vitals";
proc print data=A15001.A01_new_vitals noobs;
run;  
Title;

Output:
Learning: 


  • How to read input from another dataset
  • How to use missing() function to identify missing value
  • Using DO-END along with IF-ELSE-ELSE IF to do a set of tasks in more than one step





Problem 2: 

Modify this program so that a new variable, SumSales, representing Sales to date, is
added to the data set. Be sure that the missing value for Sales in month 3 does not

result in a missing value for SumSales.

Solution:

data A01_monthsales;
 input month sales @@;
 SumSales + Sales; * sum function ignores missing value;
datalines;
1 4000 2 5000 3 . 4 5500 5 5000 6 6000 7 6500 8 4500
9 5100 10 5700 11 6500 12 7500
;
 
Title "Monthly Sales Report";
proc print data=A01_monthsales; run;
Title;

Output:





Learning:


  • An Arithmetic computation can result in Missing value if at least one missing value is encountered
  • Sum function -> Variable + Increment can increment the Variable without any initialization and also can handle missing value
  • Sum function is also a good counter to use
  • @@ - Double Trailing to hold more than one line to receive input



Problem 3: Create a temporary SAS data set from these three lines of data. Each observation should contain Method (A, B, or C), and Score. There should be 30 observations in this data set. Use a DO loop to create the Method variable and remember to use a single trailing @ in your INPUT statement. Provide a listing of this data set using PROC PRINT.

Solution:
data A15001.A01_speed;
 do method='A','B','C';
  do i = 1 to 10;
    input score @;
    output;
   end; 
 end; 
 drop i;
datalines;
250 255 256 300 244 268 301 322 256 333
267 275 256 320 250 340 345 290 280 300
350 350 340 290 377 401 380 310 299 399
;  
run;

Output:
Learning:


  • Looping with Categorical Variable (Characters) as Index
  • Nested Looping (Loop inside another Loop)
  • @ - Single Trailing to hold the line for input
  • Controlling the implicit output in the DATA step with OUTPUT statement


Download the code here

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Blogger templates

Total Pageviews

Powered by Blogger.

- Copyright © nulldata -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -