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
You can define an array with an undetermined number of elements. This assumes all your columns start with col
.
data test;
length result $50;
set a;
array c[*] col:;
result = "";
do i=1 to dim(c);
result = Compress(catx(';',result,c[i]),'0D0A'x);
end;
drop i;
run;
col:
tells SAS that you want all variables starting with col
and the [*]
in the array tells SAS to define the number of elements itself. Use the dim()
function to get that number and loop over the values.
EDIT:
As noted in the comments, the follow approaches also work.
Without using arrays:
data test;
length result $50;
set a;
result = Compress(catx(';',of col:),'0D0A'x);
run;
Or if you still want the array:
data test;
length result $50;
set a;
array c[*] col:;
result = Compress(catx(';',of c[*]),'0D0A'x);
run;