BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Hi 

 

I need your help.

 

I have data set with four    variables;           id     test      seq     value

 data:                                                           1     DBP       1         88

                                                                    1     DBP       2        86  

                                                                    1     DBP       3          . 

                                                                    1     DBP       1         90

                                                                    1     DBP       2        86  

                                                                    1     DBP       3         88

                                                                     1     DBP      4         84

                                                                    1    SBP       1         128

                                                                    1     SBP       2        130

 

             I need to calculate average by id and all the sequence values and include in my data

 

My output should look like:          

 

                                                                   id     test      seq     value          Type

                                                                    1     DBP       1         88

                                                                    1     DBP       2        88 

                                                                    1     DBP       3          . 

                                                                    1      DBP                 88             average

                                                                    1     DBP       1         90

                                                                    1     DBP       2        86  

                                                                    1     DBP       3         88

                                                                     1     DBP      4         84

                                                                     1      DBP                 87             average

                                                                    1    SBP       1         128

                                                                    1     SBP       2        130

                                                                    1      SBP                 129              average

 

 

my code is:

 

proc transpose data=one out=two(drop=_name_);
by test;
var value;
id seq;
run;

 

data three;
set two;
four=mean(_1,_2,_3,_4);
run;


proc transpose data=three out=four;
var _1 _2 _3 _4 four;
by test;
run;

 

I am getting expected results. But I want to know any other simple appraoch. Please advise.

 

Thanks

 

 

                               

4 REPLIES 4
Reeza
Super User

I don't know if it's simpler but you could use a DOW loop. 

 

You could also use proc means and append results instead of transpose.

 

knveraraju91
Barite | Level 11

what is the code for Proc means and append the results. Thank you

Reeza
Super User

I highly highly recommend learning proc means.

You'll need to sort the results to get what you want but the following should get you started. It's obviously only a sketch of the code, again I highly recommend learning the proc. Here's a paper and you'll find many many more on lexjansen.com

 

http://www.lexjansen.com/nesug/nesug08/ff/ff06.pdf

 

 

proc means data=have;
by GROUPING_VARIABLE;
var ANALYSIS_VARIABLE;
output out=avg_value mean=NAME;
run;

data want;
set have avg_value;
run;

 

LinusH
Tourmaline | Level 20

Please don't mix aggregate values with original data in the same data set.

Your layout is a report, use a reporting tool for this. Like PROC TABULATE or REPORT.

Data never sleeps

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 4 replies
  • 2671 views
  • 2 likes
  • 3 in conversation