How can I change this macro to create all the variables in one data set? I want First_ADD and Second_ADD in the out flag data set. I want to do this for multiple varibles using the macro but it keeps overwriting itself and keeping the last flag that was created by the macro. Thank you!
%MACRO FLAG(VAR1,VAR2,FLAGNAME);
DATA FLAG;
SET RULE1;
&FLAGNAME=0;
DO _N_=1 to countw(&VAR1,',') while(&FLAGNAME=0);
word=scan(&VAR1,_N_,',');
if word=' ' then continue;
if indexw(trim(&VAR2),trim(word),',') then
&FLAGNAME=1;
end;
drop word;
run;
%MEND FLAG;
%FLAG(Addresses1,Addresses2,First_ADD);
%FLAG(Addresses1,Addresses3,Second_ADD);
Not sure what you want to get, but based on my best guess, try this:
%MACRO FLAG(IN, OUT, VAR1,VAR2,FLAGNAME);
DATA &out.;
SET &in.;
&FLAGNAME=0;
DO _N_=1 to countw(&VAR1,',') while(&FLAGNAME=0);
word=scan(&VAR1,_N_,',');
if word=' ' then continue;
if indexw(trim(&VAR2),trim(word),',') then
&FLAGNAME=1;
end;
drop word;
run;
%MEND FLAG;
%FLAG(RULE1,RULE2,Addresses1,Addresses2,First_ADD);
%FLAG(RULE2,FLAG,Addresses1,Addresses3,Second_ADD);
Bart
Not sure what you want to get, but based on my best guess, try this:
%MACRO FLAG(IN, OUT, VAR1,VAR2,FLAGNAME);
DATA &out.;
SET &in.;
&FLAGNAME=0;
DO _N_=1 to countw(&VAR1,',') while(&FLAGNAME=0);
word=scan(&VAR1,_N_,',');
if word=' ' then continue;
if indexw(trim(&VAR2),trim(word),',') then
&FLAGNAME=1;
end;
drop word;
run;
%MEND FLAG;
%FLAG(RULE1,RULE2,Addresses1,Addresses2,First_ADD);
%FLAG(RULE2,FLAG,Addresses1,Addresses3,Second_ADD);
Bart
Thank you! So i want both first_add and second_add to be in the out data set
Thank you!
What are "all the variables"?
Did you have version of the code that created an "all the variables" without any macro coding at all? That is the first step.
An example data set and the result with "all the variables" created for that example would be very helpful.
Hint: if you want to pass a list of values as a parameter do not use COMMAS as the macro processor will want to use them as delimiters between different parameters. Use a space and then specify the space in the SCAN and COUNTW function as a delimiter, or some other character like a |
Sorry should have been more specific. I want First_ADD and Second_ADD in the out flag data set.
One simple change is to NOT hardcode the names of the datasets. Instead make those as parameters.
%MACRO FLAG(in,out,VAR1,VAR2,FLAGNAME);
DATA &out;
SET ∈
&FLAGNAME=0;
DO _N_=1 to countw(&VAR1,',') while(&FLAGNAME=0);
word=scan(&VAR1,_N_,',');
if word=' ' then continue;
if indexw(trim(&VAR2),trim(word),',') then
&FLAGNAME=1;
end;
drop word;
run;
%MEND FLAG;
%FLAG(RULE1,ADD1,Addresses1,Addresses2,First_ADD);
%FLAG(ADD1 ,ADD2,Addresses1,Addresses3,Second_ADD);
You could also add a %DO loop to generate separate blocks of code for each variable so you can do it in a single data step. So instead of passing in one VAR2 name you could pass in a list.
How do you want the flag names to be generated? Do you want to also pass in a list?
Or perhaps it would be easier to just append a suffix to the name of the VAR2 variable?
%MACRO FLAG(in,out,VAR1,VARNAMEs,suffix);
%local i var2 flagname;
DATA &out;
SET ∈
%do i=1 %to %sysfunc(countw(&varnames,%str( )));
%let var2=%scan(&varnames,&i,%str( ));
%let flagname=&var2._&suffix;
&FLAGNAME=0;
DO _N_=1 to countw(&VAR1,',') while(&FLAGNAME=0);
word=scan(&VAR1,_N_,',');
if word=' ' then continue;
if indexw(trim(&VAR2),trim(word),',') then
&FLAGNAME=1;
end;
%end;
drop word;
run;
%MEND FLAG;
options mprint;
%FLAG(RULE1,ADD,Addresses1,Addresses2 Address3,ADD);
If that is not what you want then you probably need to supply some example input and the output you want from that input to help explain what you are doing.
Thank you! That did help me fir=gure it out
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.