SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Vic3
Obsidian | Level 7

I use the following code to calculate means, etc. and add them to a dataset:

proc means data=mydata;
	by class;
	var var1;
	output out=avg_value mean=average Q1=q25 Q3=q75;
run;
data new_data;
	set mydata avg_value;
run;

What I get is a "stacked" dataset such as this:

Class Var1 _TYPE_ _FREQ_ average q25 q75
a 2 . . . . .
a 4 . . . . .
a 3 . . . . .
a 4 . . . . .
b 9 . . . . .
b 8 . . . . .
b 6 . . . . .
b 7 . . . . .
a . 0 4 3.25 2.75 4
b . 0 4 7.5 6.75 8.25

 

What I need is a dataset with those statistics in every row for a corresponding class, such as this:

Class Var1 average q25 q75
a 2 3.25 2.75 4
a 4 3.25 2.75 4
a 3 3.25 2.75 4
a 4 3.25 2.75 4
b 9 7.5 6.75 8.25
b 8 7.5 6.75 8.25
b 6 7.5 6.75 8.25
b 7 7.5 6.75 8.25

 

Thank you.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Try this:

data new_data;
	merge mydata avg_value;
    by class;
    drop _type_ _freq_;
run;

 

Which brings up the questions: why create this data set at all? What are you going to do next once you have it? Are you going to determine where each value of VAR1 exists between the p25 and p75? Please explain, very often this data set is not necessary, there may be easier ways to getting to the next step (if only we knew what the next step is, we could provide those easier ways).

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Try this:

data new_data;
	merge mydata avg_value;
    by class;
    drop _type_ _freq_;
run;

 

Which brings up the questions: why create this data set at all? What are you going to do next once you have it? Are you going to determine where each value of VAR1 exists between the p25 and p75? Please explain, very often this data set is not necessary, there may be easier ways to getting to the next step (if only we knew what the next step is, we could provide those easier ways).

--
Paige Miller
Vic3
Obsidian | Level 7
This worked. Thank you.
I want to identify various outliers (1.5*IQR, 3*IQR) among other things.
PaigeMiller
Diamond | Level 26

@Vic3 wrote:

I want to identify various outliers (1.5*IQR, 3*IQR) among other things.

You don't need PROC MEANS or this data set NEW_DATA. SAS has a PROC that will do exactly what you want. This should work for you:

 

proc stdize data=mydata method=iqr out=want;
    by class;
    var var1;
run;
--
Paige Miller
Vic3
Obsidian | Level 7
Thank you, I'll explore proc stdize.

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 794 views
  • 0 likes
  • 2 in conversation