BookmarkSubscribeRSS Feed
ari
Quartz | Level 8 ari
Quartz | Level 8

Have: 

IDDayR
1  
1  
1Day 1 
1Day 2run 0
1  
1  
1  
1  
1Day1run 1
1  
1Day2run2
1  
1  

 

Want:

IDDayR
1  
1  
1Day 1 
1Day 2run 0
1 run 0
1 run 0
1 run 0
1 run 0
1Day1run 1
1 run 1
1Day2run2
1 run2
1 run2

 

I want to retain the value of R based on the value of Day (R value carried forward until the next Day

). Any help to achieve this using the retain function?

 

Thanks,

4 REPLIES 4
data_null__
Jade | Level 19

This is pretty standard LOCF.

 

data run;
   infile cards missover;
   input ID Day &$ run &$;
   cards;
1 
1 
1 Day 1
1 Day 2  run 0
1 
1 
1 
1 
1 Day1  run 1
1 
1 Day2  run 2
1 
1 
;;;;
   run;
proc print;
   run;
data run;
   if 0 then set run;
   update run(keep=id obs=0) run(keep=id run);
   by id;
   set run(keep=day);
   output;
   run;
proc print;
   run; 

Capture.PNG

ari
Quartz | Level 8 ari
Quartz | Level 8

@data_null__. It works but difficult to understand to modify to copy the remaining variables as such in the dataset. My original dataset contains 50 variables and those be copied as such. Any pointers? Thanks

data_null__
Jade | Level 19

@ari wrote:

@data_null__. It works but difficult to understand to modify to copy the remaining variables as such in the dataset. My original dataset contains 50 variables and those be copied as such. Any pointers? Thanks


 

Did you do any reading in the SAS online documentation about the statements used in the program?

 

change

 

set run(keep=day);

to

set run(drop=id run);

All variables in RUN except ID and RUN are added but their values are not carried forward.

 

The SET statement is to bring in all the variables that you do not want have values carried forward.

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1455 views
  • 1 like
  • 2 in conversation