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

Hello everyone,

 

I am trying to solve this problem but I haven't been able to. Please help.

 

%let thisvar = BUS;

%let buslist = A B C D E;

 

%macro A;

%do k = 1 %to %sysfunc(countw(&buslist));

%let list = %scan(&bustlist, &k)

%let xlist = %sysfunc(cats(&list,1_,&thisvar));

 

** I don't know how to do this part ***

 

%end;

%mend;

%A;

 

What I would like to have in the end is:

 

A1_BUS B1_BUS C1_BUS D1_BUS E1_BUS into some macro variable (say %let BUS=);

 

Please help. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @tonoplast,

 

Usually, SAS functions such as CATS are not needed to concatenate text in macro language. You could simplify the %DO loop to

%do k = 1 %to %sysfunc(countw(&buslist));
  %let finlist = &finlist %scan(&buslist, &k)1_&thisvar;
%end;

 Edit: Further simplified as per @Astounding's reminder.

View solution in original post

6 REPLIES 6
tonoplast
Obsidian | Level 7

Looks like I figured it out. If there is an easier way / better way than this, please let me know (this probably needs re-work from the beginning). Thank you.

 

%let thisvar = BUS;

%let buslist = A B C D E;

%let finlist=;

 

 

%macro A;

%do k = 1 %to %sysfunc(countw(&buslist));

%let list = %scan(&bustlist, &k)

%let xlist = %sysfunc(cats(&list,1_,&thisvar));

 

%let finlist = %sysfunc(cats(&finlist. &xlist));

 

%end;

%mend;

%A;

 

%put &finlist;

 

heffo
Pyrite | Level 9

Some typos in the original question! But tonoplasts code works exactly as it should. 

%let thisvar = BUS;
%let buslist = A B C D E;
%let finlist=;

%macro A;
	%do k = 1 %to %sysfunc(countw(&buslist));
		%let list = %scan(&buslist, &k);
		%let xlist = %sysfunc(cats(&list,1_,&thisvar));
		%let finlist = %sysfunc(cats(&finlist. &xlist));
	%end;
%mend;
%A;

%put &finlist;

 

FreelanceReinh
Jade | Level 19

Hello @tonoplast,

 

Usually, SAS functions such as CATS are not needed to concatenate text in macro language. You could simplify the %DO loop to

%do k = 1 %to %sysfunc(countw(&buslist));
  %let finlist = &finlist %scan(&buslist, &k)1_&thisvar;
%end;

 Edit: Further simplified as per @Astounding's reminder.

Astounding
PROC Star
One more small simplification: remove the %LEFT function. The %LET statement will automatically ignore leading blanks.
FreelanceReinh
Jade | Level 19

Oh yes, of course! Thanks for reminding me. (This was the result of too quickly switching from a version using a trailing blank.)

tonoplast
Obsidian | Level 7

Thank you, everyone!!

 

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 916 views
  • 2 likes
  • 4 in conversation