BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kyle234
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

7 REPLIES 7
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



kyle234
Obsidian | Level 7

Thank you! So i want both first_add and second_add to be in the out data set

ballardw
Super User

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 |

kyle234
Obsidian | Level 7

Sorry should have been more specific. I want  First_ADD and Second_ADD in the out flag data set.

Tom
Super User Tom
Super User

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.

 

kyle234
Obsidian | Level 7

Thank you! That did help me fir=gure it out

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 857 views
  • 0 likes
  • 4 in conversation