简体   繁体   中英

how to excute sas macro iteratively in another macro?

i would like to get result of brand_channel macro. macro is not working on i=2,3,4 in %do-loop statement. How can I execute doing_scoring macro iteratively?

thanks!

%doing_scoring;
...
...
...
%mend doing_scoring;

%macro brand_channel;
proc sql noprint;
    create table oneb_onec as
        select unique x1, x2  
           from mydata_all;
    quit;

data seq_oneb_onec;
set oneb_onec;
seqno = _N_;
run;

%let num=4;
%do i=1 %to  #
    %put doing number is &i;
    %put end doing number is #

proc sql noprint;
    create table onebc_table&i as
        select    a.* 
        from       mydata_all a, seq_oneb_onec b
        where    b.seqno = &i
            and    b.x1 = a.x1
            and    b.x2 = a.x2;
        quit;
%doing_scoring(mydata=onebc_table&i, setnumber = &i);
%end;

%mend brand_channel;

%brand_channel;

Your code is fine, except for the initial line (declaration of doing_scoring), but that's likely transcription error I suppose. Below I have a functional test version.

However, I have a better way to do the same thing. Fundamentally, macro driven iteration is a bad idea; there is a better way to do almost every task you might want to attempt.

In this case, you can call the doing_scoring calls directly from the seq_ dataset, and either move the creation of the sub-dataset to the macro (should be easy) or, perhaps better, keep the dataset in one piece.

First the better way: call execute. (Or, you can create the macro calls in SQL using select into .)

proc sort data=sashelp.class out=class;
by age sex;
run;

%macro doing_scoring(data=,age=,sex=,setnumber=);
data mydata;
set class;
where age=&age. and sex="&sex.";
run;

*whatever else you are doing;
%mend doing_scoring;

data _null_;
set class;
by age sex;
if first.sex then seqno+1; 
callstr=cats('%doing_scoring(data=class,age=',age,',sex=',sex,',setnumber=',seqno,')');
call execute(callstr);
run;

Now, the original way with same test data.

%macro doing_scoring(mydata=,setnumber=);
%put doing_scoring &mydata. &setnumber.;
%mend doing_scoring;

%macro brand_channel;
proc sql noprint;
    create table oneb_onec as
        select distinct age,sex
           from sashelp.class;
    quit;

data seq_oneb_onec;
set oneb_onec;
seqno = _N_;
run;

%let num=4;
%do i=1 %to  #
    %put -------------------;
    %put doing number is &i;
    %put end doing number is #

proc sql noprint;
    create table onebc_table&i as
        select    a.* 
        from       sashelp.class a, seq_oneb_onec b
        where    b.seqno = &i
            and    b.age = a.age
            and    b.sex = a.sex;
        quit;
%doing_scoring(mydata=onebc_table&i, setnumber = &i);
%put -------------------;
%end;

%mend brand_channel;

%brand_channel;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM