## Macro variable with multiple values

Solved
Occasional Contributor
Posts: 6

# Macro variable with multiple values

Hi There,

I am having  Macro var1 having multiple values seperated by delimitor (master list of charcter type variable), another macro var2 val seperated by dilimitor (another subgroup of Mixed type (Char+Num) ). i want to find out the common values between two macro variables. any one please help me.

Macro Var1 = Name sex race occp     {all character type variables}

Macro Var2 = Name age sex salary race country {mixed type char and Num variables}

Solution Like  Macro Var3 = Name sex race   {only characters type vars

Thanks

Accepted Solutions
Solution
‎09-22-2017 12:19 AM
Super User
Posts: 7,939

## Re: Macro variable with multiple values

So you have these macro variables.

``````%let charvars = Name sex race occp ;
%let allvars = Name age sex salary race country ;
``````

And you want to produce.

``````%let found=Name sex race;
``````

It would be easier to do it with a data step (if the values of the macro variables are not longer than 32K characters).

``````data _null_;
charvars="&charvars";
allvars="&allvars";
length found \$32767 ;
do i=1 to countw(charvars,' ');
if indexw(upcase(allvars),upcase(scan(charvars,i,' '))) then
found=catx(' ',found,scan(charvars,i,' '))
;
end;
call symputx('found',found);
run;``````

Although you could do it inside of a macro.  You could even define the macro to work like a function since it has no need to generate SAS code.

``````%macro findwords(allvars,charvars);
%local i next found ;
%do i=1 %to %sysfunc(countw(&charvars,%str( )));
%let next=%scan(&charvars,&i,%str( ));
%if %sysfunc(indexw(%upcase(&allvars),%upcase(&next),%str( ))) %then
%let found=&found &next
;
%end;
&found.
%mend findwords;
``````

Then you could call it to create your list.

``````%let found=%findwords(&allvars,&charvars);
%put &=found ;``````

All Replies
Regular Contributor
Posts: 202

## Re: Macro variable with multiple values

[ Edited ]

EDIT: this post can be ignored ... should not post without sufficient caffeine consumed.

With the names alone it is impossible to tell whether a variable is numeric or alphanumeric. You need the names of dataset and library and query sashelp.vcoulmn or another metadata-source.

You can select variables depending on there type from sashelp.column.

Untested code:

``````proc sql;
select *
from sashelp.vcolumn
where libname = 'SASHELP' and MemName = 'CLASS' and Type = 'char'
;
quit;``````

Solution
‎09-22-2017 12:19 AM
Super User
Posts: 7,939

## Re: Macro variable with multiple values

So you have these macro variables.

``````%let charvars = Name sex race occp ;
%let allvars = Name age sex salary race country ;
``````

And you want to produce.

``````%let found=Name sex race;
``````

It would be easier to do it with a data step (if the values of the macro variables are not longer than 32K characters).

``````data _null_;
charvars="&charvars";
allvars="&allvars";
length found \$32767 ;
do i=1 to countw(charvars,' ');
if indexw(upcase(allvars),upcase(scan(charvars,i,' '))) then
found=catx(' ',found,scan(charvars,i,' '))
;
end;
call symputx('found',found);
run;``````

Although you could do it inside of a macro.  You could even define the macro to work like a function since it has no need to generate SAS code.

``````%macro findwords(allvars,charvars);
%local i next found ;
%do i=1 %to %sysfunc(countw(&charvars,%str( )));
%let next=%scan(&charvars,&i,%str( ));
%if %sysfunc(indexw(%upcase(&allvars),%upcase(&next),%str( ))) %then
%let found=&found &next
;
%end;
&found.
%mend findwords;
``````

Then you could call it to create your list.

``````%let found=%findwords(&allvars,&charvars);
%put &=found ;``````

Super User
Posts: 6,641

## Re: Macro variable with multiple values

Excellent advice.  Also note, you can get rid of the local macro variable FOUND.  Remove it from the %LOCAL statement, remove the final &FOUND. line, and make this change:

%if %sysfunc(indexw(%upcase(&allvars),%upcase(&next),%str( ))) %then &next;

Occasional Contributor
Posts: 6

## Re: Macro variable with multiple values

Hi Tom

Thanks for your time to solve the issue, i appriciate your help.

Frequent Contributor
Posts: 113

## Re: Macro variable with multiple values

[ Edited ]

Try this....

``````%let Var1 = Name sex race occp;
%let Var2 = Name age sex salary race country;

data
STRING1 (keep=string1  where=(string1 is not missing) )
STRING2 (keep=string2  where=(string2  is not missing) );

VAR1=upcase("&var1");
Var1wc=countw(var1);

var2=upcase("&var2");
var2wc=countw(var2);

do i=1 to max(var1wc,var2wc);
length string1 string2 \$32;

string1 =scan(var1,i," ");
string2 =scan(var2,i," ");
output;
end;

run;

proc sql noprint;
select coalescec(string1 ,string2 ) into: var3 separated by " ";
from WANT A , WANT B
where string1= string2 ;
quit;

``````

Hope this helps.

Occasional Contributor
Posts: 6

## Re: Macro variable with multiple values

Hi ShiroAmanda,

Thank you for your time, Code was simple and easy to understand to every one. i appriciate your help.

☑ This topic is solved.