BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BaileyY
Obsidian | Level 7
Hi.
 
Can anyone help me write a loop that will run through a dataset and for each row for each ID - stop at column variable 'Summer' and take the information in that column and put into a new column called 'New_String'.  Each running loop would input that new information value from 'Summer' and add onto the next byte under the new column.  Example below: 
 
ID #2516 has three records in this dataset.  Each record has a different value in column 'Summer'.  Notice in column 'New_String' each of those values are concatenated together to create value 'ABD'  
 
Is this doable?  Thank you for any help! 
Data:    
ID Summer Price
2516 A 269
1678 A 240
20982 B 125
2516 B 452
20982 A 315
2516 D 804
20982 D 804
20982 A 200
WANT:    
ID NEW_STRING  
2516 ABD  
1678 A  
20982 BADA  
     
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
proc sql;
create index id on have(id);
quit;

data want;
 do until(last.id);
  set have;
  by id;
  length want $50;
  want=cats(want,summer);
 end;
 keep id want;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @BaileyY  Very straight forward

 

data have;
input ID	Summer $	Price;
cards;
2516	A	269
1678	A	240
20982	B	125
2516	B	452
20982	A	315
2516	D	804
20982	D	804
20982	A	200
;

data _null_ ;
if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("id") ;
   h.definedata ("id","want") ;
   h.definedone () ;
end;
set have end=z;
if h.find() ne 0 then want=Summer;
else want=cats(want,summer);
h.replace();
if z;
h.output(dataset:'want');
run;
novinosrin
Tourmaline | Level 20
proc sql;
create index id on have(id);
quit;

data want;
 do until(last.id);
  set have;
  by id;
  length want $50;
  want=cats(want,summer);
 end;
 keep id want;
run;
BaileyY
Obsidian | Level 7
omg!! Thank you so much. This worked perfectly!!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1424 views
  • 1 like
  • 2 in conversation