BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mohdfaisal89
Calcite | Level 5
data want;
set DMS.Dms1;
length newvar $100; /* Please adapt length as appropriate. */
newvar=catt(', ', of _A:);
run;

Please Help !

 

I want to concatenate all the columns ending with _A in a table. I have more then 60 columns ending with _A in a table of 90 columns

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data testdata;
input (ID FirstVar SecondVar ThirdVar One_A Two_A Three_A)(:$10.);
datalines;
1 1 2 3 1 2 3
2 4 5 6 4 5 6
3 7 8 9 7 8 9
4 1 2 3 1 2 3
5 4 5 6 4 5 6
;

%let suffix=A;
 
proc sql noprint;
	select name into :vars separated by ',' 
	from dictionary.columns 
	where upcase(libname)=upcase('work') 
	  and upcase(memname)=upcase('testdata')
	  and upcase(substr(name,length(name)-(length("&suffix")-1),length("&suffix")))=upcase("&suffix");
quit;
 
%put &vars;

data want;
	set testdata;
   newvar=cats(&vars);
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

You will need to retrieve the variable names from dictionary.columns:

data have;
input col1 $ col1_a $ col2_a $;
cards;
x y z
;
run;

proc sql noprint;
select trim(name) into :vars separated by ','
from dictionary.columns
where libname = 'WORK' and memname = 'HAVE'
  and upcase(substr(name,length(name)-1,2)) = '_A'
;
quit;

data want;
set have;
length newvar $100;
newvar = catx(',',&vars);
run;

proc print data=want noobs;
run;

Result:

col1    col1_a    col2_a    newvar

 x        y         z        y,z  
PeterClemmensen
Tourmaline | Level 20
data testdata;
input (ID FirstVar SecondVar ThirdVar One_A Two_A Three_A)(:$10.);
datalines;
1 1 2 3 1 2 3
2 4 5 6 4 5 6
3 7 8 9 7 8 9
4 1 2 3 1 2 3
5 4 5 6 4 5 6
;

%let suffix=A;
 
proc sql noprint;
	select name into :vars separated by ',' 
	from dictionary.columns 
	where upcase(libname)=upcase('work') 
	  and upcase(memname)=upcase('testdata')
	  and upcase(substr(name,length(name)-(length("&suffix")-1),length("&suffix")))=upcase("&suffix");
quit;
 
%put &vars;

data want;
	set testdata;
   newvar=cats(&vars);
run;
mohdfaisal89
Calcite | Level 5

Thank you ! Worked Great!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 3 replies
  • 1128 views
  • 0 likes
  • 3 in conversation