BookmarkSubscribeRSS Feed
itshere
Obsidian | Level 7

Hi - 

 

using proc sql;

 

Im trying to make a query that says, if the call happened within 7 days after the case creation then add a 1, otherwise 0 and if the call happened within 31 days after the case creation add a 1, otherwise 0. 

 

the date is in the following format 01MAY2016 11:10:00 (datetime20) both case and call. 

 

this is my formula currently but it didnt work correct. 

 

proc sql;
create table test.rock as
select A.*,
(case when datepart( A.call)>datepart(a.case)+7 then 1 else 0 end) as call_in_7days,
(case when datepart( A.call)>datepart(a.case )+31 then 1 else 0 end) as call_in_31days
from test.paper A

;

 

but the results were incorrect. please advise if my logic is incorrect. 

3 REPLIES 3
Reeza
Super User

Look at cases where it didn't work. 

What were the values of Call and Case in those cases? Also, should your comparison be > or >=?

 

Your logic is incorrect though. 

 

You want the Call to be between Case and Case+7 , your checking if it's more than 7 days, which is a Call more than 7 days after a Case Creation. 

ChrisNZ
Tourmaline | Level 20

I find

 

proc sql;
create table test.rock as
select A.*,
(case when datepart( A.call)-datepart(a.case) > 7 then 1 else 0 end) as call_in_7days,
(case when datepart( A.call)-datepart(a.case ) > 31 then 1 else 0 end) as call_in_31days
from test.paper A

;

 

easier to read. And then you see your mistake. You probably mean

 

create table test.rock as
select A.*,
(case when datepart( A.call) - datepart(a.case) <= 7 then 1 else 0 end) as call_in_7days,
(case when datepart( A.call) - datepart(a.case ) <= 31 then 1 else 0 end) as call_in_31days
from test.paper A

 

which can also be written

 

select A.*,
( datepart( A.call) - datepart(a.case) <= 7 ) as call_in_7days,
( datepart( A.call) - datepart(a.case ) <= 31 ) as call_in_31days
from test.paper A

 

 

PGStats
Opal | Level 21

Simplify your code and make it independent of internal date and time representation. Use SAS date and time interval functions (intnx and intck). The "DTDAY" interval represents a day for datetime values. 

 

proc sql;
create table test.rock as
select 
	*,
	intck("DTDAY", case, call) <= 7  as call_in_7days,
	intck("DTDAY", case, call) <= 31  as call_in_31days
from test.paper;
PG

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