Calcite | Level 5

## Interpolated Median Syntax (Macro & Loop)

Dear SAS Base Gurus,

I am trying to work something out which I am sure SAS can do but not sure how to continue with incorporating loop within macro.

Herewith the background as follows:

1. Got this code (IMReplicateForum.sas) from mr.google;
``````/*HAD THIS MACRO BELOW FROM SAS MASTER SIFU VIA GOOGLE*/
%macro intmed(dset, var);

proc means data=&dset. median noprint;
var &var.;
output out=outmed median=median;
run;

data _null_;
set outmed;
call symput("Median", median);
run;

data transform;
set &dset. end=last;

retain nl ne ng 0;
median = &median.*1;

if &var. < median then nl = nl+1;
if &var. = median then ne = ne+1;
if &var. > median then ng = ng+1;

if last then do;
if ne ne 0 then do;
intmed = median + (ng-nl)/(2*ne);
end;
else if ne = 0 then do;
intmed = median;
end;
output;
end;
label median = "Median"
intmed = "Interpolated Median";
keep median intmed;
run;

proc print data = transform label noobs;
title "Value for Median and Interpolated Median";
var median intmed;
run;
title "";
%mend;

data Survey1 (keep=Question1AreUSatisfy);
set Survey;
if Question1AreUSatisfy; /*GET RID NULL/BLANK*/
if ProgramID eq "MATH101";
run;
%intmed (Survey1, Question1AreUSatisfy);

*AT PRESENT THE MACRO JUST SHOWIN UP (OUTPUT) INTERPOLATED MEDIAN (IM) FIGURES ONE AT THE TIME BASED ON DATA SET.
*AS I AM SAS NEWBIE, I AM LOOKING ENLIGHTENMENT HOW TO DO THIS RATHER THAN 'MANUALLY' SUBSET DATA & POPULATE "IM" FIGURES.``````
2. The macro run perfectly to produce Interpolated Median figures from Survey dataset (survey.sas7bdat) attached.

The macro generate Interpolated Median (IM) figures in output one at a time based on unique ProgramID (Primary Key) subset data.

What I try to achieve populate IM figures automatically in the list by add in new IM field consist figure from macro (e.g.: rather than hard coding IM figure one at a time back into dataset).

As I am newbie with limited knowledge of SAS macro and loop, any enlightenment will be appreciated.

4 REPLIES 4
PROC Star

## Re: Interpolated Median Syntax (Macro & Loop)

Should the program automatically process every numeric variable?

Should a value of zero be excluded from the median calculations?
Calcite | Level 5

## Re: Interpolated Median Syntax (Macro & Loop)

I think so, but unfortunately it's not the case.

I have to remove blank (e.g.: null) in data steps.

Super User

## Re: Interpolated Median Syntax (Macro & Loop)

Look up CALL EXECUTE(). The documentation has an example of how to use it.
Calcite | Level 5

## Re: Interpolated Median Syntax (Macro & Loop)

Shall deep dive & learn with it - thanks.
Discussion stats
• 4 replies
• 726 views
• 0 likes
• 3 in conversation