2
votes

I have a SAS dataset with values

yyyymm
201605
201606
201607
201608
201609

I am trying to find a way to pass these values one at a time to macro such that

do while dataset still has value
%macro passdata(yyyymm);
end

How can I do this in SAS. Can someone please help with a sample/code snippet.

2
What you're doing in the above sounds more like you don't need a macro and just need a normal data step. data want; set your_dataset; run; seems like it does exactly that. Can you explain more why that does not work for your needs? What is %passdata doing?Joe
Look at CALL EXECUTE. The documentation probably has exactly the sample you're looking for, otherwise, here's a fully coded example gist.github.com/statgeek/beb97b1c6d4517dde3b2Reeza

2 Answers

3
votes

As mentioned by the prior comment, a way to pass parameters is through call execute routine. Note that this must be done in datastep environment. The lines are read from the set you input.

You can input multiple variables. Just add more variables in '||' separators. Note that the variables may have a lot of whitespaces in them. (==Do comparisons with care.)

Here is a small sample code. Tested.

data start_data;
    input date_var ;
    datalines;
        201605
        201606
        201607
        201608
        201609
        ;
run;

%macro Do_stuff(input_var);
    %put 'Line generates value ' &input_var;
%mend do_stuff;

data _null_;
    set start_data;
    call execute('%do_stuff('||date_var||')'  );
run;
0
votes

Try this example and try modifying to meet your needs... From the "source" dataset we can use call symput() to assign a macro token to each observation (differentiated by the SAS automatic dataset variable n so My_token1, My_token2, etc.) Once you have a set of macro variables defined, just loop through them! This program will print all the individual records from source to the SAS log:

data source;
do var=1000 to 1010;
output;
end;
run;

data _null_;
set source;
call symput(compress("My_token"||_n_),var);
run;

%put &my_token1  &my_token4;

%Macro neat;

%do this=1 %to 11;

*Check the log.;
%put &&My_token&this;

%end;

%mend;
%neat;