BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nouf2
Calcite | Level 5

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

3 REPLIES 3
ballardw
Super User

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).

Nouf2
Calcite | Level 5

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. 

 

 

Tom
Super User Tom
Super User

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 422 views
  • 0 likes
  • 3 in conversation