Desktop productivity for business analysts and programmers

Concatenating macro variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

Concatenating macro variables

%let have1 = var1 var2 var3;
%let have2 = a b c;
%let want = a.var1=b.var1=c.var1 a.var2=b.var2=c.var2 a.var3=b.var3=c.var3;

Codes above used to generate input and output data.

 

I'm thinking about using 2 loops, and something like:

%macro sample;

%do _i = 1 %to %sysfunc(countw(&have1));

%let want&_i = ... /* e.g. want1 = a.var1=b.var1=c.var1 */

%end;

%global want;
%let want = ... /* e.g. &want1 || &want2 || &want3 */

%mend sample;
%sample;

%put &want;
run;

Because have1 and have2 can have a different number of words, hardcoding will not be accepted.

If someone can show me at least how to generate &want1 that'd be awesome!

 


Accepted Solutions
Solution
‎07-21-2017 02:57 AM
Super User
Posts: 7,782

Re: Concatenating macro variables

Do it in a data step:

data _null_;
length outstring $100 intstring $30;
have1 = 'var1 var2 var3';
have2 = 'a b c';
do i = 1 to countw(have1);
  intstring = '';
  do j = 1 to countw(have2);
    intstring = catx('=',intstring,trim(scan(have2,j))!!'.'!!trim(scan(have1,i)));
  end;
  outstring=catx(' ',outstring,intstring);
end;
call symput('want',trim(outstring));
run;
%put &want;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎07-21-2017 02:57 AM
Super User
Posts: 7,782

Re: Concatenating macro variables

Do it in a data step:

data _null_;
length outstring $100 intstring $30;
have1 = 'var1 var2 var3';
have2 = 'a b c';
do i = 1 to countw(have1);
  intstring = '';
  do j = 1 to countw(have2);
    intstring = catx('=',intstring,trim(scan(have2,j))!!'.'!!trim(scan(have1,i)));
  end;
  outstring=catx(' ',outstring,intstring);
end;
call symput('want',trim(outstring));
run;
%put &want;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 82

Re: Concatenating macro variables

Posted in reply to KurtBremser
YOU ARE A GENIUS! Smiley Happy
Super User
Super User
Posts: 7,955

Re: Concatenating macro variables

"Because have1 and have2 can have a different number of words, hardcoding will not be accepted." - This is nonsense.  SAS, SQL and such like are data based programming languages, i.e. they are specifically designed to work with data.  If you have an unknown amount of join variables, then these should not be variables at all, they should be data rows.  Joins should be hardocded in - i.e. your process shouldl know the data it is dealing with otherwise you will never know what you will get out or if it even worked.  Its a fundamental basic of data based programming, know your data, model your data appropriately.  I need to merge X datasets, data looks like:
VAR1 VAR2 VAR3...

1        4        2

I do not know the number of these, so proc transpose to normalise them to get:
VAR_NO   RESULT

1               1

2               4

3               2

...

Then I merge the data based on var_no=var_no plus any other identifers.  Then if I really need a normalised dataset, then I proc transpose the data back up (or array the data back depending on the types involved).  Simple, no loops, no guess work, no spaghetti code which falls over every other time its run, just plain simple Base SAS and data modelling.

Super User
Posts: 10,028

Re: Concatenating macro variables

%let have1 = var1 var2 var3;
%let have2 = a b c;

%macro sample;
%global want;
%let want= ;
%do _i=1 %to %sysfunc(countw(&have1));
 %let dim==;
 %do _j=1 %to %sysfunc(countw(&have2));
   %if &_j=%sysfunc(countw(&have2)) %then %let dim=%str( ) ; 
   %let want=&want%scan(&have2,&_j).%scan(&have1,&_i)&dim;
 %end;
%end;
%mend sample;
%sample

%put &want;


☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 612 views
  • 3 likes
  • 4 in conversation