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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 2230 views
  • 0 likes
  • 3 in conversation