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

Hello.  I have a data set with person-level wage data.  Each person's record also includes a state, month, and year variable.

 

I would like to be able to generate the median wage for each state in a given month and then create a new variable with that value. 

 

Is there a way to do this directly via code and avoid copying from the output window?

 

I am using SAS 9.3.

 

Thank you for any help you can provide.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic

 

Yes, PROC MEANS has an OUTPUT statement that allows you to capture the output data. 

You may need to tweak the BY statement to get exactly what you want but the follow should help you get started.

 

proc means data=have noprint;
BY STATE YEAR MONTH;
var wage;
output out=want median(wage) = median_wage;
run;

 


@Mica27 wrote:

Hello.  I have a data set with person-level wage data.  Each person's record also includes a state, month, and year variable.

 

I would like to be able to generate the median wage for each state in a given month and then create a new variable with that value. 

 

Is there a way to do this directly via code and avoid copying from the output window?

 

I am using SAS 9.3.

 

Thank you for any help you can provide.


 

View solution in original post

8 REPLIES 8
kiranv_
Rhodochrosite | Level 12

you can do this by doing output statement. I have copied an example for you for below link

http://www2.sas.com/proceedings/sugi29/240-29.pdf

 

PROC MEANS DATA=SUGI.ELEC_ANNUAL NOPRINT;
VAR TOTREV TOTKWH TOTHRS;
OUTPUT OUT=SUGI2 sum(TOTREV TOTKWH) = sum_rev sum_kwh
mean(TOTREV) = mean_rev
median(TOTHRS) = median_hrs;
run;
Mica27
Calcite | Level 5

Thank you very much!  I will try this out.

Mica27
Calcite | Level 5
Thanks for your help.
Reeza
Super User

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic

 

Yes, PROC MEANS has an OUTPUT statement that allows you to capture the output data. 

You may need to tweak the BY statement to get exactly what you want but the follow should help you get started.

 

proc means data=have noprint;
BY STATE YEAR MONTH;
var wage;
output out=want median(wage) = median_wage;
run;

 


@Mica27 wrote:

Hello.  I have a data set with person-level wage data.  Each person's record also includes a state, month, and year variable.

 

I would like to be able to generate the median wage for each state in a given month and then create a new variable with that value. 

 

Is there a way to do this directly via code and avoid copying from the output window?

 

I am using SAS 9.3.

 

Thank you for any help you can provide.


 

yanshuai
Quartz | Level 8

Hello,

 

can I use CLASS rather than BY command?

And How can I put the median_wage back to the original dataset? In another way, just add another variable containing the value I obtained from this MEANS step.

Reeza
Super User

@yanshuai wrote:

Hello,

 

can I use CLASS rather than BY command?

 


Try it and see. They are not exact replications of each other, so if you use CLASS you need to make other changes as well.

 


@yanshuai wrote:

 

And How can I put the median_wage back to the original dataset? In another way, just add another variable containing the value I obtained from this MEANS step.


See the examples here:

https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas

yanshuai
Quartz | Level 8

Thank you Reeza!

I have been using SQL or MERGE to do this. But I am trying to find if new variable can be put back to the old table directly without creating new tables.

It seems it is inevitable.

Thank you all the same.

Reeza
Super User

The last SQL step does it automatically, but if you don't have SAS 9.4+ the median will not be correct. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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