DATA Step, Macro, Functions and more

Conditional logic in proc means?

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Conditional logic in proc means?

/*Hi SAS Forum,

I have posted this same question in another forum and am repeating here too due to urgency of resutls generation.


I am having below dataset.
For variable "income", value 999 means the income of the person is actually $999.
However, for "Age" varaible, the value 999 means the value is unknown or missing. */



Data have;
input income Age;
cards;
100 999
200 25
999 30
;
run;


/*Q: I want to calcualte min, max and mean of these 2 vairables using proc means.
We cannot use other methods but must use proc means for calculating min, max and mean (otherwise
it upsets our original long SAS code).

Answer:

for income, min=100  max=999  mean=433
for Age min=25  max=30  mean=27.5  (because value 999 should be omitted from Age variable)

I attmepted below code, but getting me no where. Could an expert help me.
Thanks, Mirisa*/


%macro any (field);
data want;
set have;

%if &field ="income" %then %do;
proc means data=have;
  var &field;
run;
%end;

%mend any;
%any (income);
%any (Age);


Accepted Solutions
Solution
‎08-15-2017 08:06 PM
Super User
Posts: 6,642

Re: Conditional logic in proc means?

You should change your data, so that the 999 don't mean "missing".  For example:

 

data want;

set have;

if age=999 then age=.U;

run;

 

Then you can use a simple PROC MEANS:

 

proc means data=want;

var income age;

run;

 

By using a special missing value (.U), you can easily differentiate which values used to be 999, vs. which are missing for some other reason.  And PROC MEANS knows enough to omit any type of missing value from the calculations.

View solution in original post


All Replies
Solution
‎08-15-2017 08:06 PM
Super User
Posts: 6,642

Re: Conditional logic in proc means?

You should change your data, so that the 999 don't mean "missing".  For example:

 

data want;

set have;

if age=999 then age=.U;

run;

 

Then you can use a simple PROC MEANS:

 

proc means data=want;

var income age;

run;

 

By using a special missing value (.U), you can easily differentiate which values used to be 999, vs. which are missing for some other reason.  And PROC MEANS knows enough to omit any type of missing value from the calculations.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 383 views
  • 2 likes
  • 2 in conversation