DATA Step, Macro, Functions and more

How to concatenate strings in different observations

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

How to concatenate strings in different observations

Hi All,

Following is my requirement:

Sample dataset:

ID    Grade Batchname

ID1   A       North

ID1   B       South

ID1   A       West

ID1   C       South

ID2   A       East

ID2   D       North

ID2   D       West

Required dataset:

ID    Grade   Batchname

ID1    A       North,West

ID1    B       South

ID1    C       South

ID2    A       East

ID2    D       North,West

The batchname has to concatenate, and the grouping has to be done based on the two columns ID and Grade.

Kindly help me to get the required dataset.



Accepted Solutions
Solution
‎08-27-2013 06:21 AM
Regular Contributor
Posts: 151

Re: How to concatenate strings in different observations

infile cards;

informat id $3. grade $1. batchname $5.;

input ID Grade Batchname;

cards;

ID1 A North

ID1 B South

ID1 A West

ID1 C South

ID2 A East

ID2 D North

ID2 D West

;

proc sort data=have; by id grade; run;

data want;length b1 $20.; retain b1;

set have; by id grade;

if first.grade then b1=batchname;

else b1=catt(b1,',',batchname);

if last.grade;

drop batchname; rename b1=batchname;

run;

View solution in original post


All Replies
Solution
‎08-27-2013 06:21 AM
Regular Contributor
Posts: 151

Re: How to concatenate strings in different observations

infile cards;

informat id $3. grade $1. batchname $5.;

input ID Grade Batchname;

cards;

ID1 A North

ID1 B South

ID1 A West

ID1 C South

ID2 A East

ID2 D North

ID2 D West

;

proc sort data=have; by id grade; run;

data want;length b1 $20.; retain b1;

set have; by id grade;

if first.grade then b1=batchname;

else b1=catt(b1,',',batchname);

if last.grade;

drop batchname; rename b1=batchname;

run;

Occasional Contributor
Posts: 9

Re: How to concatenate strings in different observations

Hi Oleg_L,

Thanks for providing the solution. its working for me.

Regular Contributor
Posts: 195

Re: How to concatenate strings in different observations

proc sort data = have;

  by id grade;

run;

proc transpose data = have out = want(drop = _name_);

  by id grade;

  var batchname;

run;

data want(drop = col1 col2);

  set want;

  length batchname $60;

  if col2 = " " then batchname = col1;

  else batchname = compress(col1 || "," || col2);

run;

-Urvish

Occasional Contributor
Posts: 9

Re: How to concatenate strings in different observations

Hi UrvishShah,

Thanks for your suggestion. Transposing the variables to obervations seems good. But if there are more than two columns it wont work. In my scenario the columns can be i.e., Batchname values are not limited, it can be anything. So even dynamically doing that would be complex.

I will make use of your suggestions for concatenating observations when the values are limited. Thanks!

Super User
Super User
Posts: 6,500

Re: How to concatenate strings in different observations

Create a new variable and concat all the values for the same grouping into.

proc sort nodupkey ;

  by id grade batchname;

run;

data want ;

  do until (last.grade);

    set have ;

    by id grade ;

    length new $50 ;

    new=catx(',',new,batchname);

  end;

  drop batchname;

  rename new=batchname;

run;

Occasional Contributor
Posts: 9

Re: How to concatenate strings in different observations

Hi Tom,

Thanks for providing the solution.

Its working for me now!

Respected Advisor
Posts: 3,124

Re: How to concatenate strings in different observations

One step Hash() option to avoid explicit presorting:

data have;

infile cards;

informat id $3. grade $1. batchname $5.;

input ID Grade Batchname;

cards;

ID1 A North

ID1 B South

ID1 A West

ID1 C South

ID2 A East

ID2 D North

ID2 D West

;

data _null_;

  if _n_=1 then do;

    declare hash h(ordered:'y');

    h.definekey('id','grade');

      h.definedata('id','grade','_batch');

      h.definedone();

   end;

   length _batch $ 50;

   call missing (_batch);

   set have end=last;

   rc= h.find();

      _batch=catx(',',_batch,batchname);

rc=h.replace();

        if last then rc=h.output(dataset:'want(rename=_batch=batchname');

run;

Haikuo

Occasional Contributor
Posts: 9

Re: How to concatenate strings in different observations

Hi Haikuo,

Thanks for giving me the solution.  Its working fine. Thank you.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 337 views
  • 10 likes
  • 5 in conversation