DATA Step, Macro, Functions and more

How do I apply a macro program repeatedly?

Reply
Senior User mh
Senior User
Posts: 1

How do I apply a macro program repeatedly?

I am using this macro to generate means, frequencies, and percentages for a number of survey questions/items. Each survey question gets its own dataset and all response options (0, 1, or 2) get their own dummy variable within each dataset. Right now I apply the macro to each survey question (A1, B1, B2_1,...H9) individually (see code at the bottom). If there are 100 survey questions that would result in 100 repetitions. How could I apply the macro repeatedly in a simpler/more efficient way? 

 

%macro calc(var);

data &var. (keep=&var. v2 v1 v0);
            set tbl ;

v2=0 ; v1=0 ; v0=0 ;
if &var.=0 then v0=1 ;
else if &var.=1 then v1=1 ;
else if &var.=2 then v2=1 ;
else if &var.=. then do ;

            v2=. ;

            v1=. ;

            v0=. ;

            end ;

run ;

proc summary data=&var.;
            var &var. v2 v1 v0 ;
            output out=&var.x (keep= &var. v2 v1 v0 N sum)
            mean= &var. v2 v1 v0
            N=N
            sum=sum ;;
run ;

%mend calc ;

 

%calc(A1);

%calc(B1);

%calc(B2_1);

etc.

 

Thanks!

Occasional Contributor
Posts: 6

Re: How do I apply a macro program repeatedly?

[ Edited ]
SAS Super FREQ
Posts: 496

Re: How do I apply a macro program repeatedly?

Here is an example of a macro that calls proc code one time for each variable in the data set.  You could adapt it for your purposes.  There are many other ways to attack this.

proc contents data=sashelp.class out=name noprint; run;

data _null_; set name nobs=n; call symputx('n', n); stop; run;

%macro loop;
   %do i = 1 %to &n;
      data _null_; i = &i; set name point=i; call symputx('var', name); stop; run;
      proc print data=sashelp.class;
         var &var;
      run;
   %end;
%mend;
%loop   
Super User
Super User
Posts: 9,211

Re: How do I apply a macro program repeatedly?

Or you could use Base SAS:

data tbl;
  a1=1; b1=0; b2_1=2; row=1; output;
  a1=2; b1=1; b2_1=1; row=2; output;
run;
proc transpose data=tbl out=inter;
  by row;
  var a1--b2_1;
run;
proc sort data=inter;
  by _name_;
run;
data inter;
  set inter;
  v0=ifn(col1=0,1,0);
  v1=ifn(col1=1,1,0);
  v2=ifn(col1=2,1,0);
run;
proc summary data=inter;
  by _name_;
  var col1 v2 v1 v0;
  output out=want (keep=_name_ col1 v2 v1 v0 N sum)
          mean=col1 v2 v1 v0
          N=N
          sum=sum;
run;
Regular Contributor
Posts: 227

Re: How do I apply a macro program repeatedly?

[ Edited ]

here is an scl solution

 

you'll need to modify your macro parameters to use this

 

but otherwise, it can be used to call any macro for every variable in a data set.

 

options mprint source2;
*include 'freqhilo.sas';
%let libname = sashelp;
%let memname = class;
%let macro_name = mymacro;
%let macro_name = echo;

%macro echo(libname=,memname=,name=,type=,length=,varnum=);
%put _local_;
%mend;
*echo(libname=1,memname=2,name=3,type=4,length=5,varnum=6);
*endsas;
%let _dsid   = %sysfunc(open (&libname..&memname,i));
%let _n_obs  = %sysfunc(attrn(&_dsid,nobs));
%let _n_vars = %sysfunc(attrn(&_dsid,nvar));
%let _rc     = %sysfunc(close(&_dsid));
%put echo &=_n_obs &=_n_vars;
DATA _null_;
     attrib name   length = $32
            type   length = $1
            length length = 8
            statement length = $128;
_dsid = open("&libname..&memname");

do varnum = 1 to &_n_vars;
   name   =         varname  (_dsid,varnum) ;
   type   = lowcase(vartype  (_dsid,varnum));
   length =         varlength(_dsid,varnum);
   statement = catt
   ('%',"&macro_name"
      ,'(name='  ,name
      ,',type='  ,type
      ,',length=',length
      ,',varnum=',varnum
      ,",libname=&libname,memname=&memname)"
   );
   put statement;
   call execute(catt('%nrstr(',statement,')'));
   end;
_rc = close(_dsid);

run;

Ron Fehd

this program is based on my SESUG.2017 paper

Advanced Programming Concepts

Cardinality Ratios and Types

 

code in the paper will be available after Nov 4.

 

http://www.sascommunity.org/wiki/Cardinality_Ratios_and_Types

 

Ask a Question
Discussion stats
  • 4 replies
  • 92 views
  • 0 likes
  • 5 in conversation