SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Ranjeeta
Pyrite | Level 9

data EVTQBP EVTnotQBP;
merge EVT.EVT_hometime(in=INfullcohort) EVT.Evt_qbp(in=INQBP);
by CIHI_KEY;
if INfullcohort =1 and INQBP=0 then output EVTnotQBP;
ELSE output EVTQBP;
run;
EVT.Home time data set has 696 keys and EVT.Evt_qbp has 687 keys
I am trying to write out the 9 keys in the EVTnotQBP dataset and want to know if my code is doing it correctly?
Also in the EVTQBP dataset I want to write out the keys that are only present in the EVT.Evt_qbp dataset which has 687 observations
Is my code correct to get the desired output?

7 REPLIES 7
mkeintz
PROC Star

This comment assumes your have one record per key in each data set.

 

Just because hometime has 696 unique key values and evt_qbp has 687 values does NOT mean there will be 9 keys in EVTnotQBP ... UNLESS  you know that evt_qbp is a proper subset of hometime (i.e. no keys in evt_qbp that are not also in evt_hometime).  

 

As to your code, it should work, but you don't really need the INfullcohort dummy.  You could just:

 

data EVTQBP EVTnotQBP;
  merge EVT.EVT_hometime EVT.Evt_qbp (in=inqbp);
  by CIHI_KEY;
  if INQBP = 0 then output EVTnotQBP;
  ELSE output EVTQBP;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ranjeeta
Pyrite | Level 9

Why is it tat i dont need the infull cohort marker ?

 

mkeintz
PROC Star

If you know that evt_qbp is a proper subset of evt_hometime, then having in= dummy for evt_hometime would result in that dummy getting a value of 1 for every merged observation.  I.e. it would effectively be a constant, and offer no discriminatory power.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ranjeeta
Pyrite | Level 9
EVT QBp is a proper subset of hometime as I got 0 observations for the following if condition
else if inqbp = 1 and infullcohort=0 then output; Now if I need the cases that are only in QBP then saying if INfullcohort =1 and INQBP=1 then output; would be correct right?
mkeintz
PROC Star

You've got all the tools presented to you.  It's time to experiment on sample data.   For this question, I suspect that will be far more valuable than an answer from even the most instructive forum response.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ranjeeta
Pyrite | Level 9
Thanks
PGStats
Opal | Level 21

It would be safer to account for every possibility:

 

data evt_qbp_only evt_hometime_only evt_qbp_hometime;
merge EVT.EVT_hometime(in=INfullcohort) EVT.Evt_qbp(in=INQBP);
by CIHI_KEY;
if INfullcohort =1 and INQBP=0 then output evt_hometime_only;
if INfullcohort =0 and INQBP=1 then output evt_qbp_only;
if INfullcohort =1 and INQBP=1 then output evt_qbp_hometime;
run;
PG

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 1194 views
  • 5 likes
  • 3 in conversation