Hi,
I have a scenario where i have following dataset for example and I want to concatenate variable col2 vertically and get the desired output:
Input table -
col1 | col2 |
1 | A |
2 | B |
3 | C |
Output table:
col3 |
ABC |
BC |
C |
I have tried following code through self join but not getting the desired output, please if someone could help here:
Code:
proc sql;
create table new as
select cats(a.col2,b.col2) as final
from old as a left join old as b
on b.col1=a.col1+1;
quit;
Using basic tools:
data have;
do col1 = 1 to 3;
col2 = byte(rank("A") + col1 - 1);
output;
end;
run;
proc sort data=have; by descending col1; run;
data temp;
set have;
length col3 $16;
retain col3;
col3 = cats(col2, col3);
run;
proc sort data=temp out=want(keep=col3); by col1; run;
proc print data=want; run;
Using basic tools:
data have;
do col1 = 1 to 3;
col2 = byte(rank("A") + col1 - 1);
output;
end;
run;
proc sort data=have; by descending col1; run;
data temp;
set have;
length col3 $16;
retain col3;
col3 = cats(col2, col3);
run;
proc sort data=temp out=want(keep=col3); by col1; run;
proc print data=want; run;
Is this a real-world-problem or something else?
How does the real data look like?
Are there three obs always?
data have;
input col1 col2 $;
datalines;
1 A
2 B
3 C
;
data want(keep = col3);
do _N_ = 1 by 1 until (z);
set have end = z;
length s $200;
s = cats(s, col2);
end;
do _N_ = 1 to _N_;
set have;
col3 = substr(s, _N_);
output;
end;
run;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: