- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Oleg_L,
Thanks for providing the solution. its working for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Tom,
Thanks for providing the solution.
Its working for me now!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Haikuo,
Thanks for giving me the solution. Its working fine. Thank you.