DATA Step, Macro, Functions and more

problem in looping macro in sas programming

New Contributor
Posts: 4

problem in looping macro in sas programming

hay, i have some problem. i want to looping all macro, and expected output is i have 3 dataset output from proc freq (test_education ; test_residence and test_monthly_income)

but my code works for the last variable in macro %LET (monthly_income)..

is it possible to me if i want to automatically create 3 dataset in row.. how is it possible? is my sas code is wrong?


sorry for my bad english.. here i attach the code...

%macro infoid(data,xvar,yvar);

data _null_;

if 0 then set &data.;

array _x

  • &xvar;
  • array _y

  • &yvar;
  • call symput("nx", trim(left(put(dim(_x),12.))));

    call symput("ny", trim(left(put(dim(_y),12.))));

    length _mname _vname $20;

    do _i=1 to dim(_x);


    call execute('%global '||_mname);

    call vname(_x[_i],_vname);

    call symput(_mname,trim(_vname));


    do _i=1 to dim(_y);


    call execute('%global '||_mname);

    call vname(_y[_i],_vname);

    call symput(_mname,trim(_vname));



    proc sort data=&data.(keep= &&x&nx gbi ) out=test_&&x&x;

    by &&x&nx;



    %let xvar = education_level residence monthly_income ;


    Super User
    Posts: 5,881

    Re: problem in looping macro in sas programming

    I'm confused, you have three parameters in the macro definition, but you only seem to use two of them, and I guess that your &xvar will be received as &data - is that what you want?

    Data never sleeps
    Super User
    Super User
    Posts: 8,114

    Re: problem in looping macro in sas programming

    There is no actual macro loop coded in that macro.

    It looks like you want to put the PROC SORT step inside of a %DO loop.

    Super Contributor
    Posts: 339

    Re: problem in looping macro in sas programming


    If I've understood your macro function definition it processes one data set only, so maybe you could call your macro 3 times, each time supplying the different data set name.

    To automatically loop through a space-separated list of data set names then you could use something like the following, based on code supplied by @Tom:

    %macro infoid_loop(datalist);

      %local i date ;

      %do i=1 %to %sysfunc(countw(&datalist,%str( )));

        %let dataset=%scan(&datalist,&i,%str( ));



    %mend infoid_loop;

    Then invoke it:

    %infoid_loop(set_a set b set_c,arg2,arg3);

    One thing I noticed is that your macro function definition allows 3 parameters to be passed to it:

    %macro infoid(data,xvar,yvar);

    but you are only calling it with 2 parameters:


    If you are still having problems then please post the log with any error messages showing.



    Ask a Question
    Discussion stats
    • 3 replies
    • 4 in conversation