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!

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

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