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

Dear experts,

Could I request you to help me to solve my problem. 

Input dataset

ID testbase1baseflag
1Liver50Y
1Liver  
1Liver  
1Lung  
1Lung80Y
1Lung  
2wight  
2wight  
2wight70Y
2wight  
2wight  

 

I need to retain basline flag values within the id and test. 

Output required

ID testbase1baseflagbase
1Liver50Y50
1Liver  50
1Liver  50
1Lung   
1Lung80Y80
1Lung  80
2wight   
2wight   
2wight70Y70
2wight  70
2wight  70

 

I tried with below code. But it did not work. 

 

data test1;
set test;
by id test;
retain BASE_;
if not missing(base) then Base_=base;
else Base=Base_;
run;

Please suggest.

Thanks,

Rjy9

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID test $ base1 baseflag $;
infile datalines missover;
datalines;
1 Liver 50 Y
1 Liver     
1 Liver     
1 Lung      
1 Lung 80 Y 
1 Lung      
2 wight     
2 wight     
2 wight 70 Y
2 wight     
2 wight     
;

data want;
   set have;
   by ID test;
   if first.test then base = .;
   if base1 then base = base1;
   retain base;
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input ID test $ base1 baseflag $;
infile datalines missover;
datalines;
1 Liver 50 Y
1 Liver     
1 Liver     
1 Lung      
1 Lung 80 Y 
1 Lung      
2 wight     
2 wight     
2 wight 70 Y
2 wight     
2 wight     
;

data want;
   set have;
   by ID test;
   if first.test then base = .;
   if base1 then base = base1;
   retain base;
run;
RJY9
Calcite | Level 5

Thank you so much for your very quick response. It solved my problem 🙂

Tom
Super User Tom
Super User

Note that this

if base1 then base = base1;

does not work right when BASE1 is zero since SAS considers zero and missing as FALSE.

Either use 

if not missing(base1) then base = base1;

Or more simply

base = coalesce(base1,base);

 

 

RJY9
Calcite | Level 5

Thank you 🙂

Kurt_Bremser
Super User

You need to reset at a group change:

data test1;
set test;
by id test;
retain base;
if first.test then call missing(base);
if not missing(base1) then base = base1;
run;
RJY9
Calcite | Level 5

Thank you so much for your very quick response. Much appreciated 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1946 views
  • 0 likes
  • 4 in conversation