BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kavitha
Calcite | Level 5

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.


1 ACCEPTED SOLUTION

Accepted Solutions
Oleg_L
Obsidian | Level 7

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

8 REPLIES 8
Oleg_L
Obsidian | Level 7

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;

Kavitha
Calcite | Level 5

Hi Oleg_L,

Thanks for providing the solution. its working for me.

UrvishShah
Fluorite | Level 6

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

Kavitha
Calcite | Level 5

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!

Tom
Super User Tom
Super User

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;

Kavitha
Calcite | Level 5

Hi Tom,

Thanks for providing the solution.

Its working for me now!

Haikuo
Onyx | Level 15

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

Kavitha
Calcite | Level 5

Hi Haikuo,

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 8 replies
  • 2488 views
  • 10 likes
  • 5 in conversation