DATA Step, Macro, Functions and more

How to populate a group variable values to all obs

Reply
Super Contributor
Posts: 272

How to populate a group variable values to all obs

Dear,

For sorting a dataset I need to populate the decod values all relavent OBS . Please help. Thank you very much

 

output needed;

Bodsys     decod         sev 

blsd                            mild
blsd                          moderate
blsd                          severe
blsd        anae
blsd       anae           mild
blsd     anae             moderate
blsd      anae            severe
blsd       lymp
blsd      lymp             mild
blsd      lymp           moderate
blsd      lymp             severe
;

data one;
input bodsys $4. +1 decod $4. +1 sev $8.;
datalines;
blsd      mild
blsd      moderate
blsd      severe
blsd anae  
blsd      mild
blsd      moderate
blsd      severe
blsd lymp 
blsd      mild
blsd      moderate
blsd      severe
;
PROC Star
Posts: 7,363

Re: How to populate a group variable values to all obs

data want (drop=hold);;
  set one;
  retain hold;
  if not missing(decod) then hold=decod;
  decod=hold;
run;

Art, CEO, AnalystFinder.com

 

Super Contributor
Posts: 272

Re: How to populate a group variable values to all obs

Dear,

 

Thank you very much for the code.You code works for me for the sample data I posted. But if the bodsys values increase it is not getting the output i need.

 In the below example, The decod value='lymph' is populating for next OBS where bodsys='clsd'

Output needed;

 

output needed;

Bodsys     decod         sev 

blsd                            mild
blsd                          moderate
blsd                          severe
blsd        anae
blsd       anae           mild
blsd     anae             moderate
blsd      anae            severe
blsd       lymp
blsd      lymp             mild
blsd      lymp           moderate
blsd      lymp             severe

clsd                            mild
clsd                          moderate
clsd                          severe
clsd       cnae
clsd      cnae           mild
clsd     cnae            moderate

clsd     cnae            severe

 


data one; input bodsys $4. +1 decod $4. +1 sev $8.; datalines; blsd mild blsd moderate blsd severe blsd anae blsd mild blsd moderate blsd severe blsd lymp blsd mild blsd moderate blsd severe clsd mild clsd moderate clsd severe clsd cnae blsd mild blsd moderate blsd severe ;

data want;
set one;
retain hold;
if not missing(decod) then hold=decod;
decod=hold;
run;

 

output getting;

Bodsys     decod         sev 

blsd                            mild
blsd                          moderate
blsd                          severe
blsd        anae
blsd       anae           mild
blsd     anae             moderate
blsd      anae            severe
blsd       lymp
blsd      lymp             mild
blsd      lymp           moderate
blsd      lymp             severe

clsd      lymp             mild
clsd     lymp              moderate
clsd     lymp              severe
clsd       cnae
clsd      cnae           mild
clsd     cnae            moderate

clsd     cnae            severe

PROC Star
Posts: 7,363

Re: How to populate a group variable values to all obs

You didn't show what you want in such a situation, but it might be:

data want (drop=hold);
 set one;
 length hold $4;
 by bodsys notsorted;
 retain hold;
 if first.bodsys then call missing(hold);
 if not missing(decod) then hold=decod;
 decod=hold;
run;

Art, CEO, AnalystFinder.com

Ask a Question
Discussion stats
  • 3 replies
  • 126 views
  • 3 likes
  • 2 in conversation