BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Hi guys, 

suppose to have the following: 

 

data DB;
input ID :$20. Discharge :date9. Discharge_n :date9.;
format Discharge :date9. Discharge_n :date9.;
cards;
0001 13FEB2019 .
0001 01FEB2019 28FEB2019
0002 01FEB2020 .
0002 04FEB2020 28FEB2020
;

then you want to update Discharge (all the fields, missings and not) with the variable Discharge_n to get: 

data DB1;
  input ID :$20. Discharge :date9. Discharge_n :date9.;
  format Discharge date9. Discharge_n :date9.;
cards;
0001 28FEB2019     .      
0001 28FEB2019  28FEB2019
0002 28FEB2020     .
0002 28FEB2020  28FEB2020
;

If I use the rule: if discharge_n > discharge only the corresponding row will be updated but I would like to update all rows corresponding to the same ID. 

 

Can anyone help me please? 

 

Thank you in advance

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data DB;
input ID :$20. Discharge :date9. Discharge_n :date9.;
format Discharge :date9. Discharge_n :date9.;
cards;
0001 13FEB2019 .
0001 01FEB2019 28FEB2019
0002 01FEB2020 .
0002 04FEB2020 28FEB2020
;
data want;
do until(last.id);
 set DB;
 by id;
 if last.id and discharge_n > discharge then do;flag=1;temp=Discharge_n;end;
end;
do until(last.id);
 set DB;
 by id;
 if flag then do;
   if not missing(Discharge) then Discharge=temp;
   if not missing(Discharge_n) then Discharge_n=temp;
   output;
 end;
end;
drop flag temp;
run;

View solution in original post

2 REPLIES 2
ballardw
Super User

One way:

data dd;
input ID :$20. DD :date9.   ;
datalines;
0001 28FEB2019
0002 28FEB2020
;

data want;
   merge db dd (In=InDD);
   by id;
   if indd then do;
      if not missing (discharge) then discharge=dd;
      if not missing (discharge_n) then discharge_n=dd;
   end;
   drop dd;
run;

The question is where the knowledge of which ID and which date to update the values with may come from as that was not stated anywhere that I could see.

 

I provided a different ID to show that a subset of ID's can be addressed this way.

Ksharp
Super User
data DB;
input ID :$20. Discharge :date9. Discharge_n :date9.;
format Discharge :date9. Discharge_n :date9.;
cards;
0001 13FEB2019 .
0001 01FEB2019 28FEB2019
0002 01FEB2020 .
0002 04FEB2020 28FEB2020
;
data want;
do until(last.id);
 set DB;
 by id;
 if last.id and discharge_n > discharge then do;flag=1;temp=Discharge_n;end;
end;
do until(last.id);
 set DB;
 by id;
 if flag then do;
   if not missing(Discharge) then Discharge=temp;
   if not missing(Discharge_n) then Discharge_n=temp;
   output;
 end;
end;
drop flag temp;
run;

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 648 views
  • 1 like
  • 3 in conversation