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.
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
;
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;
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
;
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;
Hi Oleg_L,
Thanks for providing the solution. its working for me.
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
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!
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;
Hi Tom,
Thanks for providing the solution.
Its working for me now!
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
Hi Haikuo,
Thanks for giving me the solution. Its working fine. Thank you.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: