BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ayin
Quartz | Level 8
%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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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;
ayin
Quartz | Level 8
YOU ARE A GENIUS! 🙂
RW9
Diamond | Level 26 RW9
Diamond | Level 26

"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.

Ksharp
Super User
%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;


Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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