Now you do in reverse!You again restructure a data set, but this time you are going from a data set with multiple observations per subject to a data set with a single observation per subject. Your original data set OLD contains one observation for each subject-time combination, with a measurement value, X, as follows:
You want a data set NEW that contains one observation per subject, with all measurements on each record, as follows:
XI in data set NEW is the value of X at time 1 in data set OLD, etc. Here's how to do it:
ExamplePROC SORT DATA=OLD; H
This program uses a number of powerful SAS System programming features.You start by sorting the data by SUBJECT, and TIME within SUBJECT 0.If you are really sure the data set is already in SUBJECT- TIME order, the sort is unnecessary. Next, you create a new data set NEW by reading in observations from data set OLD 0. In general, a BY statement following a SET statement sets up special temporary variables called FIRST.
fryvar and LAST.fryvar for each of the variables in the BY list.They are temporary because they are not saved with the data set, but rather they exist for the duration of the DATA step only; FIRST.fryvar has a value of 1(true) for every observation that is the first occurrence of a new BY group, and a value of 0 (false) for all other occurrences. Likewise, LAST.byvar has a value of 1 for the last occurrence of a BY group,and a value of 0 for all other occurrences.
In our example, the temporary variables work as follows. After being sorted, data set OLD exists in the sorted order of TIME within SUBJECT. Using the BY statement following the SET statement in the DATA step, you instruct the SAS System to create the internal variables FIRST.SUBJECT and LAST.SUBJECT. Each time you begin a new SUBJECT BY group (begin reading observations for a new subject), the variable FIRST.SUBJECT 0 is true (set equal to 1).
At all other times, FIRST.SUBJECT is false (equal to 0).When you encounter the last observation for a subject, LAST.SUBJECT 0 is true (1). At all other times, it is false (0).You use these logical (true or false) FIRST.fryvar and LAST.byvar variables to accomplish different goals.
The program works by accumulating values for all times for each subject from OLD before outputting an observation to NEW. It's a little tricky, so pay attention. A RETAIN statement © is used to "remember" the values of X1-X3 until it's time to output them.When the first observation from OLD is read, SUBJECT=1, TIME=1, X=5, FIRST. SUBJECT=1, and LAST.SUBJECT=0. The elements of ARRAY XX, (XI, X2, X3)are initialized to missing (.) 0. Although this initialization step is not necessary here, if you are missing an observation for one or more times for a subject, you would retain the values from the previous subject if you did not initialize XI to X3 to missing.This initialization is done each time a new subject is read in (FIRST.SUBJECT=1). Statement © instructs the program to set the element of XX with a subscript equal to the current value of TIME, equal to the current value of X.
For the first observation,TIME=1 and X=5.Since TIME = 1, XX[TIME] is the same as XX which represents the variable XI (set equal to 5, the value of X).
The next observation from OLD is then read (SUBJECT=1, TIME=2, X=6, FIRST.SUBJECT=0, and LAST.SUBJECT=0.) Values of X1-X3 are retained from the previous observation (Xl=5, X2=., X3=.) and XX[TIME], or XX, or X2, is set equal to 6, the current value of X in OLD. This happens again for the next observation resulting in Xl=5, X2=6, and X3=7.This time however, since LAST.SUBJECT ® is true (equal to 1),the observation is ouput to NEW. Only the variables SUBJECT, XI, X2, and X3 are kept 0. Th process is then repeated for SUBJECT 2.
And (Weight in KG) = (Weight in pounds) / 2.2.
SAS data set FOURPER:
Hint: Use another variable in the DO loop and compute TREAT inside the loop.
Notes: 1. There are no missing values in the data set.
2. The data set is already sorted in ID order.
SAS Programming Related Tutorials
|SASS (Syntactically Awesome Style sheets) Tutorial||R Programming language Tutorial|
SAS Programming Related Interview Questions
|Logistics Interview Questions||SAS Programming,SQL server Interview Questions|
|Clinical SAS Interview Questions||SASS (Syntactically Awesome Style sheets) Interview Questions|
|SQL Server Analysis Services (SSAS) Interview Questions||R Programming language Interview Questions|
|SAS DI Interview Questions||Advanced SAS Interview Questions|
|Base Sas Interview Questions||SAS Macro Interview Questions|
|Clinical Data Management Interview Questions|
Sas Programming Tutorial
Input And Infile
Set, Merge, And Update
Table Lookup Tools
Proc Means And Proc Uimivariate
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.