Hello,
I am new to SAS and I'm facing a problem figuring out:
How to create a new variable, which is the average of two existed variables (e.g. new variable=SBP, existed variable1=SPB1, existed variable2=SPB2)
Thank you.
So if your existing dataset is named HAVE then data step will generate a new dataset named WANT that has the new SBP variable.
data want;
set have;
sbp=mean(sbp1,sbp2);
run;
So if you also DBP1 and DBP2 then you just add another line to create DBP also.
data want;
set have;
sbp=mean(sbp1,sbp2);
dbp=mean(dbp1,dbp2);
run;
It really helps to provide a bit more structured details on the question. I am not sure whether you mean for each observation in a data set or of summarized data across the entire data set or a subset within the data.
If you want the mean on each observation of a data set then in a data step use the mean function.
data example; x=4; y=8; average=mean(x, y); run;
Mean, and the associated other data step statistics functions like Min, Max, Std (standard deviation) will return the requested statistic based on the non-missing values of a list of variables (can be way more than 2).
Thank you for your reply.
I mean I have a dataset with 14 different variables, two of them are named (SBP1 and SBP2).
I want to create a new variable that it is not part of the 14 existed variables of the dataset ( I will name it SBP). This new variable has to be the mean of both SBP1 and SBP2 variables.
I believe the code has to contain some order to create a new variable then it calculate the mean to create the values of this new proposed variable.
PS: I am taking Biostatistics course and we must use SAS, I am from medical background that's why it's tough for me to understand how it works!
Thank you.
So if your existing dataset is named HAVE then data step will generate a new dataset named WANT that has the new SBP variable.
data want;
set have;
sbp=mean(sbp1,sbp2);
run;
So if you also DBP1 and DBP2 then you just add another line to create DBP also.
data want;
set have;
sbp=mean(sbp1,sbp2);
dbp=mean(dbp1,dbp2);
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.