BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Primavera
Quartz | Level 8

Hi All,

 

I have a dataset that can be simplified to the following for the purpose of my question:

ID Dispflag DRDiagnose
1 1  
2   3
2   7
2 1  
3   12
3 1  
4 1  
5   2
5   8
5 1  
6   2
6 1  
7 1  
8 1  
9   6
9   14
9   14
9 1  

 

for each ID I want to set the value of DRDiagnose to the previous value of DRDiagnose if the Dispflag is 1. If there is no previous value for DRDiagnose for that ID (like for ID 1, 7, or 😎 then the value will remain missing. Basically, I want to get the following dataset:

ID Dispflag DRDiagnose
1 1  
2   3
2   7
2 1 7
3   12
3 1 12
4 1  
5   2
5   8
5 1 8
6   2
6 1 2
7 1  
8 1  
9   6
9   14
9   14
9 1 14

 

I thought I can do it in a  few lines of code but it is not working:

data want;
set have;
by ID;
if (Dispflag eq 1) then DRDiagnose=lag(DRDiagnose);
run;

Does anyone know why my code is not working and have a fix for it or another solution to achieve the "want" dataset? Many thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
 infile cards truncover;
 input ID  Dispflag DRDiagnose;
 cards;
1 1  
2 .  3
2  . 7
2 1 
3 . 12
3 1  
4 1  
5 . 2
5 . 8
5 1  
6 . 2
6 1  
7 1  
8 1  
9 . 6
9 . 14
9 . 14
9 1  
;


data want ;
  set have ;
  lag=lag(DRDiagnose);
  if id=lag(id) and Dispflag then DRDiagnose=lag ;
  drop lag;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20


data have;
 infile cards truncover;
 input ID 	Dispflag	DRDiagnose;
 cards;
1	1	 
2	. 	3
2	 .	7
2	1	
3	.	12
3	1	 
4	1	 
5	.	2
5	.	8
5	1	 
6	.	2
6	1	 
7	1	 
8	1	 
9	.	6
9	.	14
9	.	14
9	1	 
;


data want ;
  set have ;
  by id ;
  retain t ;
  if first.id then t = . ;
  if DRDiagnose then t = DRDiagnose ;
  if Dispflag and not DRDiagnose and t then DRDiagnose = t ;
  drop t ;
run ;
Primavera
Quartz | Level 8

Hi, 

Thank you very much for your help. I tried this but it did not work. I was not able to fix it either. But using retain is an excellent idea. I was so stalked with lag that I did not even think of retain 😞

 

 

Ksharp
Super User
data have;
 infile cards truncover;
 input ID  Dispflag DRDiagnose;
 cards;
1 1  
2 .  3
2  . 7
2 1 
3 . 12
3 1  
4 1  
5 . 2
5 . 8
5 1  
6 . 2
6 1  
7 1  
8 1  
9 . 6
9 . 14
9 . 14
9 1  
;


data want ;
  set have ;
  lag=lag(DRDiagnose);
  if id=lag(id) and Dispflag then DRDiagnose=lag ;
  drop lag;
run;
Primavera
Quartz | Level 8

Hi, 

Thank you so much. All that was needed was to add that dummy variable "lag" and drop it at the end. Very interesting.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 4 replies
  • 622 views
  • 2 likes
  • 3 in conversation