BookmarkSubscribeRSS Feed
dominiquec
Calcite | Level 5

I'm having a hard time getting the code below to run at all. I am working with medication fill data, and I have two files which separately list all fills for two different medications. In the dataset 'nif2', I am trying to create a flag to indicate a washout period: within a unique ID, for each fill of medication #1 in 'nif2', I want to see if there is also a fill for medication #2 in 'allfills'  in the 120 days preceding the fill in 'nif2'.

 

data nifgroup.nif2;
set nifgroup.nif1;
amlinwash = 0;
run;

proc sql;
update nifgroup.nif2 as a
set amlinwash = 1
where exists 
(select * from amlgroup.allfills as b
where a.patid = b.patid and b.fill_dt <= a.fill_dt and b.fill_dt >= (a.fill_dt -120));
quit;

When I run this code, it does not stop (even after an unreasonable amount of time). I need to stop the code myself. When I check the log, it shows that it gets to the 'proc sql' step and the first row in 'allfills' is read, but nothing has happened after that. Any suggestions for why the code above is getting stuck is much appreciated.

2 REPLIES 2
Tom
Super User Tom
Super User

Don't use UPDATE. 

 

Seems like a very strange strategy to go to the trouble to make a dataset that you then immediately turn around and try to modify.  Just make the dataset you want directly.

proc sql;
create table nifgroup.nif2 as 
  select a.*
       , max(b.fill_dt between a.fill_dt - 120 and a.fill_dt) as amlinwash
  from nifgroup.nif1 a
  left join amlgroup.allfills b
    on a.patid = b.patid
  group by a.patid, a.fill_dt
;
quit;
dominiquec
Calcite | Level 5

Thank you for your help! When I try your code, the number of observations increased (not something I expected). What happened was that for each patid that appears in 'allfills', the number of observations for that individual in 'nif2' is the number of obs in 'nif1' multiplied by the number of obs in 'allfills'. 

 

Here's a made-up example of what I'm trying to get: 

nifgroup.nif1

patidfill_dt
10010008/05/2019
10010009/01/2019
10010009/30/2019
10010010/28/2019
10010011/29/2019

 

amlgroup.allfills

patidfill_dt
10010001/02/2019
100100

03/30/2019

10010006/25/2019
10010007/18/2019

 

nifgroup.nif2

patidfill_dtamlinwash
10010008/05/20191
10010009/01/20191
10010009/30/20191
10010010/28/20191
10010011/29/20190

 

For patid 100100, this person currently has 20 observations in nif2, with each fill_dt repeating 4 times. I need the individual to retain the original number of observations from 'nif1' when creating 'nif2', with only the creation of amlinwash added to the new dataset (as shown in table).

 

In my data, each fill_dt has a unique 'fill ID'; if I take the first unique value of the fill ID within each unique patid, I should get what I need. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 899 views
  • 1 like
  • 2 in conversation