Computing Values as Percentages of All Observations - SAS Programming

In this example, you want to express individual data values as a percentage of the mean of all subjects in a group.You have a SAS data set TEST containing variables HR (heart rate),SBP (systolic blood pressure),and DBP (diastolic blood pressure) for each subject, and you want to express these values as percents of the mean of all subjects.

For example, if the values for heart rate for the group were 80, 70, and 60, the group mean would be 70 and the first subject's HR value of 80 would yield a percentage score of 114.286% (100 x 80/70).The approach here is to use PROC MEANS to compute group means, and then to output them to a data set which you combine with the original data so that you can perform the needed computations. The code follows:

Example

DATA TEST?
INPUT HR SBP DBF;
DATALINES;
80 160 100
70 150 90
60 140 80
i
0
PROC MEANS NOPRINT DATA=TEST?
VAR HR SBP DBP;
OUTPUT OUT =MOUT
MEAN=MHR MSBP MDBP;
RUN;
DATA PERCENT ; ©
SET TEST; 0
DROP MHR MSBP MDBP _TYPE FREQ_;
IF __N_=1 THEN SET MOUT; O
RRPER -100*HR/MHR; 0
SBPPER=100*SBP/MSBP;
DBPPER=100*DBP/MDBP;
RUN;
PROC PRINT NOOBS DATA=PERCENT;
TITLE 'Listing of PERCENT Data Set';
RUN;

You use the NOPRINT option 0 with PROC MEANS because you do not want the procedure to print anything, only to create a data set of means.In this case, because there is no CLASS statement and you are processing the entire TEST data set as one entity with no subsetting,the output data set MOUT consists of only one observation.

This single observation is shown here:

Computing Values as Percentages of All Observations

You want to combine the data from MOUT (the three mean variables: MHR, MSBP,and MDBP) with the data from every observation in the original data set so that you can divide each single value by the appropriate mean (HR by MHR; SBP by MSBP; DBP by MDBP) and then multiple by 100. Since the original data set TEST has three observations and the mean data set MOUT contains only one observation,

you use a trick (that's what this SAS programming stuff is all about).You create a new data set PERCENT @ by reading in variables (via SET statements) from existing data sets TEST and MOUT.Each time you loop through the DATA step in creating another observation in PERCENT,you set another observation from the original TEST data set to bring in a new collection of variables HR, SBP, and DBP © (go back to Chapter 3, "SET, MERGE,and UPDATE," for a review of SET if needed). You only have to set the values from the MOUT variables (MHR, MSBP, MDBP) once, however, from the first (and only) observation in MOUT, and then retain the values for all new observations in PERCENT.

You do this by using the SAS internal variable _N_ (the observation counter) to execute conditionally the SET MOUT statement only once.O Since you do not set a new observation from MOUT with subsequent iterations of the DATA step, the values from the first one are automatically retained into each new obsesrvation being built in PERCENT.

This works because the SET statement contains an implicit RETAIN statement within it.Variables that are accessed via a SET statement are not reinitialized with each new observation being built. Values from the previous observation are carried forward and are replaced only with a new execution of the SET statement.

Since the SET statement here is only executed once, when _N_=1, the original values for MHR, MSBP, and MDBP are always brought forward (never replaced) and are always available. You divide HR by MHR, SBP by MSBP, and DBP by MDBP, and then multiply each quotient by 100 to get the percentages you want, HRPER, SBPPER, and DBPPER respectively.© The final data set created by this program is shown in Ouput 6:

Output from Example - Computing Values as Percentages of All Observations

Output from Example  - Computing Values as Percentages of All Observations


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics