Concatenation with array

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 117
Accepted Solution

Concatenation with array

 

Hi SAS Experts,


I have a data with name and strings, I want to add all the strings(col1,col2,etc..) into one column. The number of strings are not fixed they may be more or less sometimes. I can do this with catx but do not know how to achieve this with array. Below is my data set.  Please Guide.


data a;
input name$ col1$ col2$ col3$ col4$;
DATALINES;
Harry abc dcd vgd bvd
peter cvc fgf ghg ghh
John fgg ftg uty gfdg
sheyala fgf jty fhf fgr
;
run;


here is my code:

 

data test;
length result $50;
set a;
result=Compress(catx(';',of col1-col4),'0D0A'x);
run;

 

But the number of strings are not fixed. 

 

Thanks & Regards,

Sanjay


Accepted Solutions
Solution
‎08-23-2017 05:23 AM
Super User
Super User
Posts: 8,634

Re: Concatenation with array

Are they all called colX if so you don't need an array:

result=catx(',',of col:);

Note the colon after the col prefix.

If they are not all called that then:

array tmp{*} col1 abc def;
result=catx(',',of tmp:);

View solution in original post


All Replies
Solution
‎08-23-2017 05:23 AM
Super User
Super User
Posts: 8,634

Re: Concatenation with array

Are they all called colX if so you don't need an array:

result=catx(',',of col:);

Note the colon after the col prefix.

If they are not all called that then:

array tmp{*} col1 abc def;
result=catx(',',of tmp:);
Frequent Contributor
Posts: 117

Re: Concatenation with array

Hi RW9,

 Can you elaborate how to use this with array.

Super User
Posts: 8,590

Re: Concatenation with array


sanjay1 wrote:

Hi RW9,

 Can you elaborate how to use this with array.


There's nothing to elaborate. @RW9's post contains a complete example.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 12,148

Re: Concatenation with array


sanjay1 wrote:

Hi RW9,

 Can you elaborate how to use this with array.


Use "of arrayname(*)" where your are currently using "of col1-col4". BUT you must define the array beforehand.

Super User
Posts: 21,572

Re: Concatenation with array

If you need a solution like this, it's clearly homework so review your course notes or the documentation on how to reference "variable lists". The answer is on this page:
http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p0wphcpsfgx6o7n1sjtq...

BTW the answer on SO is incorrect. It does work, but if that's the homework question it's highly likely that's not the correct answer.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 137 views
  • 0 likes
  • 5 in conversation