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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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