Help using Base SAS procedures

Proc Means, new variable, collapse across conditional category

Reply
Contributor
Posts: 33

Proc Means, new variable, collapse across conditional category

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 

Contributor QLi
Contributor
Posts: 57

Proc Means, new variable, collapse across conditional category

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.

Contributor
Posts: 33

Proc Means, new variable, collapse across conditional category

QLi,

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

Contributor QLi
Contributor
Posts: 57

Proc Means, new variable, collapse across conditional category

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

Super User
Posts: 17,897

Re: Proc Means, new variable, collapse across conditional category

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;

Ask a Question
Discussion stats
  • 4 replies
  • 650 views
  • 0 likes
  • 3 in conversation