BookmarkSubscribeRSS Feed
asp778ACU
Calcite | Level 5

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.

 

Thanks in advance.

4 REPLIES 4
Astounding
PROC Star
Should the program automatically process every numeric variable?

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

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

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

Reeza
Super User
Look up CALL EXECUTE(). The documentation has an example of how to use it.
asp778ACU
Calcite | Level 5
Shall deep dive & learn with it - thanks.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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