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

Hello!

I am trying to assign a value to duplicate rows.  

 

This is the data I have:

Medrec Results 
771441 Growth
771441 Growth
771441 No Growth
771441 No Growth
771441 No Growth

 

This is the data I want:

Medrec Results Growth
771441 Growth Y
771441 Growth Y
771441 No Growth Y
771441 No Growth Y
771441 No Growth Y

 

This is what I've tried but I only get one row to have Growth = 'Y'

 

 
proc sort data = medrec2; by medrec results;run;
data medrec3;
set medrec2;
by medrec;
 
if first.Medrec then do;
if results = 'Growth' then growth = 'y';
else if results = 'No Growth' then growth = 'n';
end;
run;
 
Thank you!!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You forgot the RETAIN statement so that the value of GROWTH does not get set to missing at the start of each iteration of the data step.

 

Also what value to you want it to have if RESULTS is neither Growth nor No Growth?  If you don't have another ELSE clause then any MEDREC group where the first value is neither of those two very specific values will retain the value calculated for the previous group.

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

You forgot the RETAIN statement so that the value of GROWTH does not get set to missing at the start of each iteration of the data step.

 

Also what value to you want it to have if RESULTS is neither Growth nor No Growth?  If you don't have another ELSE clause then any MEDREC group where the first value is neither of those two very specific values will retain the value calculated for the previous group.

hameliz
Calcite | Level 5
Thank you!  Where would I put 'Retain'?

And there are only two options = 'Growth' or 'No Growth'.

thanks!
Tom
Super User Tom
Super User

 It just tells SAS not to reset the new variable to missing at the start of the next iteration.  So since it has no executable component you can place the retain statement any where in the data step.

 

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0t2ac0tfzcgbjn112mu96hkgg9o.htm

Tom
Super User Tom
Super User

If you only have two possible values then why not just create a binary variable.  That is easier to create

data medrec3;
  set medrec2;
  by medrec;
  if first.Medrec then growth= (results = 'Growth');
  retain growth;
run;

and easier to work with. 

 

hameliz
Calcite | Level 5

I just did some searching and added it like this

 

proc sort data = medrec2; by medrec results;run;
data medrec3;
set medrec2;
by medrec;
 
if first.Medrec then do;
retain growth;
if results = 'Growth' then growth = 'y';
else if results = 'No Growth' then growth = 'n';
end;
run;
 
and it worked!!!
 
thank you 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 942 views
  • 0 likes
  • 2 in conversation