BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

 I want to Concatenating strings from multiple rows into single row in table.

 I expect to get in last row under months_list column the value  1808+1809+1810+1811+1812+1901.

 But I don't get this desired result. 

May anyone help and explain what is the problem with code? 

 

Data tbl1;
input month $;
cards;
1808
1809
1810
1811
1812
1901
;
run;


data tbl2;
  length months_list  $ 50;
  set tbl2;
  by month;
  retain months_list;
  if first.month_Yadua then months_list = month;
  else months_list = catx('+', months_list, month);
run;

 

5 REPLIES 5
Ksharp
Super User
Data tbl1;
input month $;
cards;
1808
1809
1810
1811
1812
1901
;
run;


data tbl2;
  length months_list  $ 50;
  set tbl1;
  retain months_list;
  months_list = catx('+', months_list, month);
run;
Ronein
Onyx | Level 15

Can you please explain why don;t we need to use first statement?

 

Kurt_Bremser
Super User

@Ronein wrote:

Can you please explain why don;t we need to use first statement?

 


Because you do not want to treat every month as a single group. You want to concatenate ALL months.

ballardw
Super User

@Ronein wrote:

Can you please explain why don;t we need to use first statement?

 


For one thing it references a variable that does not exist in your example data. The FIRST. syntax also expects that the variable appears on a BY statement. Since you don't have a variable month_Yadua in the example we get an uninitialized variable note and has no effect on the output

 

 

 

Shmuel
Garnet | Level 18

Please pay attention to notes, to your code:

 

data tbl2;
  length months_list  $ 50;
  set tbl2;   /* <<<< should be tbl1, same name as created in previous step */ 
  by month;
  retain months_list;   /* <<< default value is an empty field, so no need to assign
specific value first time */ if first.month_Yadua then months_list = month; /* month_Yadua is an undefined variable
you could use first.month (asyou defined by month )
but then
each row will be "first" and no concatenation */ else months_list = catx('+', months_list, month); run;
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
  • 5 replies
  • 1348 views
  • 3 likes
  • 5 in conversation