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

Hello,

 

does anyone know if you can calculate summary statistics for multiple flags at once.?data have;
input id $ age hip $ knee $ both$;
datalines;
AB01 55 1 0 0
AB01 65 0 1 1
AC01 65 0 1 0
AC01 70 0 1 0
AC01 75 1 0 1
AD01 50 0 1 0
AD01 60 0 1 0
;

I want to do the summary statistics by age where each flag=1.

I know I can do it in 3 steps with a where condition but I am wondering if I can do it in one step and have an output like this:

median_hip p25_hip p75_hip median_knee p25_knee p75_knee median_both p25_both p75_both

 

thanks

KC

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @Kc2,

 

I see two options:

 

You can obtain the nine statistics in one step, but in the form of a 3x3 matrix (rather than a row vector):

proc summary data=have;
class both knee hip;
var age;
ways 1;
output out=want0(drop=_type_ _freq_ where=(_level_=2)) median= p25= p75= / autoname levels;
run;

Then you could use PROC TRANSPOSE and/or a DATA step to restructure dataset WANT0, if necessary.

 

Alternatively, you can first prepare a view and then get the desired result directly (with a slightly different naming convention and order of variables, though):

data vhave / view=vhave;
set have;
hip =ifn(hip, age,.);
knee=ifn(knee,age,.);
both=ifn(both,age,.);
run;

proc summary data=vhave;
var hip knee both;
output out=want(drop=_:) median= p25= p75= / autoname;
run;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

I'm afraid this is unclear. What variable do you want to compute summary statistics on?? Do you want the mean of AGE? That doesn't make sense you said you want the summary statistics BY AGE. There are no other numeric variables to compute summary statistics on. 

--
Paige Miller
ballardw
Super User

@Kc2 wrote:

Hello,

 

does anyone know if you can calculate summary statistics for multiple flags at once.?data have;
input id $ age hip $ knee $ both$;
datalines;
AB01 55 1 0 0
AB01 65 0 1 1
AC01 65 0 1 0
AC01 70 0 1 0
AC01 75 1 0 1
AD01 50 0 1 0
AD01 60 0 1 0
;

I want to do the summary statistics by age where each flag=1.

I know I can do it in 3 steps with a where condition but I am wondering if I can do it in one step and have an output like this:

median_hip p25_hip p75_hip median_knee p25_knee p75_knee median_both p25_both p75_both

 

thanks

KC

 


I think you need to provide a great deal of details on what you mean. Since you show values of 1/0 for hip and knee the median, p25 or p75 values will be either 1 or 0. Is that what you intend??? And what do you mean by "median_both"? I think that you need to be very explicit with example input data and what you would expect as the actual result for this, especially that "both".

Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

I would like to do a proc means  on the variable age and get the median, qand the 25 and 75 percentile where each of the flags are equal to 1. I can do it with a where condition in 3 steps 

 

the flags are numeric, I made a typo previously

input id $ age hip  knee  both;
datalines;
AB01 55 1 0 0
AB01 65 0 1 1
AC01 65 0 1 0
AC01 70 0 1 0
AC01 75 1 0 1
AD01 50 0 1 0
AD01 60 0 1 0
;

 

proc means data=have noprint;
where hip=1;
var age;
output out=want1(drop=_type_ _freq_) median= p25= p75= / autoname ;
run;

 

proc means data=have noprint;
where knee=1;
var age;
output out=want2(drop=_type_ _freq_) median= p25= p75= / autoname ;
run;

 

proc means data=have noprint;
where both=1;
var age;
output out=want3(drop=_type_ _freq_) median= p25= p75= / autoname ;
run;

 

Can I do it in one step , where  I get the median and the percentiles of the age variable when each flag =1?

I would get one observation:

median_hip p25_hip p75_hip      median_knee p25_knee p75_knee      median_both p25_both p75_both

FreelanceReinh
Jade | Level 19

Hello @Kc2,

 

I see two options:

 

You can obtain the nine statistics in one step, but in the form of a 3x3 matrix (rather than a row vector):

proc summary data=have;
class both knee hip;
var age;
ways 1;
output out=want0(drop=_type_ _freq_ where=(_level_=2)) median= p25= p75= / autoname levels;
run;

Then you could use PROC TRANSPOSE and/or a DATA step to restructure dataset WANT0, if necessary.

 

Alternatively, you can first prepare a view and then get the desired result directly (with a slightly different naming convention and order of variables, though):

data vhave / view=vhave;
set have;
hip =ifn(hip, age,.);
knee=ifn(knee,age,.);
both=ifn(both,age,.);
run;

proc summary data=vhave;
var hip knee both;
output out=want(drop=_:) median= p25= p75= / autoname;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 520 views
  • 0 likes
  • 4 in conversation