BookmarkSubscribeRSS Feed
jdub
Calcite | Level 5

Hi

I am trying to create a new variable that contains observations that are summed observations from another variable conditional on if the observations fall into a class.

Example:

Class: NAICS

Old variable: employment

New variable: employment_subset

logic to create new variable: employment_subset = sum of employment where NAICS in(21,22,23,..)

I have used proc means to do something similar, e.g. collapse observations by class.  Here I want to collapse observations over a specified range categories within a class.  Not sure how I would do this either with proc means or in the data step.  Can I do this with proc means?  If so, how do I condition the collapse over a range of categories within the class?

thanks 

4 REPLIES 4
QLi
Fluorite | Level 6 QLi
Fluorite | Level 6

try this one:

proc means data=  noprint;

var employment;

output out=want ( drop=_:  ) sum=employment_subset;

where naics in (21, 22, 23);

run;

Hopefully,it works out.

jdub
Calcite | Level 5

QLi,

thanks much.  what does the option { (drop=_:) } reference after the output out statement?  Does this option drop all observations not included in the sum operation?

QLi
Fluorite | Level 6 QLi
Fluorite | Level 6

the option:  (drop=_: )  is able to drop the columns (_freq_ , _type_)  in the proc means.

Reeza
Super User

You can always use a  format as well, this is useful if you want to recategorize variables without really creating a new variable.

*create a format;

proc format;

value age_fmt 12 - 14 = 'Tweens';

run;

*print results;

proc print data=sashelp.class;

format age age_fmt.;

run;

*summarize by the new formats;

proc means data=sashelp.class;

class age; var weight height;

format age age_fmt.;

run;

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