BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Brad39
Fluorite | Level 6

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 -                                

col1col2
1A
2B
3C

 

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;

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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;

PGStats_0-1662232370195.png

 

PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

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;

PGStats_0-1662232370195.png

 

PG
andreas_lds
Jade | Level 19

Is this a real-world-problem or something else?

How does the real data look like?

Are there three obs always?

PeterClemmensen
Tourmaline | Level 20
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;

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1221 views
  • 5 likes
  • 4 in conversation