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!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1013 views
  • 0 likes
  • 3 in conversation