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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.