DATA Step, Macro, Functions and more

problem in looping macro in sas programming

Reply
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?

thanks

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);

    _mname='x'||trim(left(put(_i,12.)));

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

    call vname(_x[_i],_vname);

    call symput(_mname,trim(_vname));

    end;

    do _i=1 to dim(_y);

    _mname='y'||trim(left(put(_i,12.)));

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

    call vname(_y[_i],_vname);

    call symput(_mname,trim(_vname));

    end;

    stop;

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

    by &&x&nx;

    run;

    %mend;

    %let xvar = education_level residence monthly_income ;

    %infoid(&xvar,gbi);

    Super User
    Posts: 5,260

    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: 6,502

    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: 282

    Re: problem in looping macro in sas programming

    Hi,

    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( ));

        %infoid(&dataset,arg2,arg3);

      %end;

    %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:

    %infoid(&xvar,gbi);

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

    Regards,

    Amir.

    Ask a Question
    Discussion stats
    • 3 replies
    • 189 views
    • 0 likes
    • 4 in conversation