Hi,
I am trying to create a 'Class Variable' for only observations within a specific time period (30 days before 15-May-2012 and 30 days after 16-May-2012). However, the output of my code seems to include observations that are not within this specified time period.
I'm a little confused here, I probably did not write my codes properly. Please see below:
SAS Code:
data xx;
set x;
Startdate = '15May2012'd - 30;
Enddate = '16May2012'd + 30;
If Startdate <= date then flag=1;
If Enddate > date then flag = 0;
run;
Thank you in anticipation!
Thank you, Patrick. Your suggestion was helpful.
However, I modified the code a little more to ensure variables in each class are within the specified time period.
I made use of:
data xx;
set x;
Startdate1 = '15May2011'd - 30;
Enddate1 = '15May2011'd;
Startdate2 = '16May2011'd;
Enddate2 = '16May2011'd + 30;
If Startdate1 < date < Enddate1 then flag=0;
If Startdate2 < date < Enddate2 then flag=1;
run;
This worked fine for me. I'm still learning most of these little manipulations. Thank you for your help once more!
I trued the code below too. The outputs are still kinda the same.
data xx;
set x;
Startdate = '15May2012'd - 30;
Enddate = '16May2012'd + 30;
If Startdate < date < Enddate then flag=1;
else flag = 0;
run;
Show us how is your input data. Is the data values are really stored as SAS date values?
You can also use INTNX function here like:
If INTNX("DAY",'15May2012'd,-30)< date < INTNX("DAY",'15May2012'd,30) then flag=1;
else flag = 0;
Thank you, SuryaKiran.
See below as requested:
Date | RSP |
1/4/1993 | 22 |
2/5/1999 | 44 |
11/16/2003 | 5 |
1/7/2005 | 5 |
3/18/2006 | 5 |
1/11/2007 | 63 |
2/12/2008 | 92 |
5/13/2011 | 56 |
1/14/2012 | 93 |
5/15/2012 | 6 |
5/16/2012 | 4 |
5/19/2012 | 3 |
6/20/2012 | 2 |
9/21/2013 | 45 |
I think it has something to do with the way the '+30 days' and '-30 days' were defined in the codes.
Your codes are also giving similar outputs as mine.
What format are your date values? This works fine for me, Check below
data have;
FORMAT Date mmddyy10.;
infile datalines dlm='09'x ;
input Date :mmddyy10. RSP;
datalines;
1/4/1993 22
2/5/1999 44
11/16/2003 5
1/7/2005 5
3/18/2006 5
1/11/2007 63
2/12/2008 92
5/13/2011 56
1/14/2012 93
5/15/2012 6
5/16/2012 4
5/19/2012 3
6/20/2012 2
9/21/2013 45
;
run;
data want;
Format Start_Date End_Date mmddyy10.;
set have;
Start_Date=INTNX("DAY",'15May2012'd,-30);
End_Date=INTNX("DAY",'15May2012'd,30);
If INTNX("DAY",'15May2012'd,-30)< date < INTNX("DAY",'15May2012'd,30) then flag=1;
else flag = 0;
RUN;
Thank you, Suryakiran. This was helpful too.
Your code as such looks o.k. to me except that you might also want to include the borders if it's really about +-30 days from a given date.
data xx;
set x;
Startdate = '15May2012'd - 30;
Enddate = '16May2012'd + 30;
If Startdate <= date <= Enddate then flag=1;
else flag = 0;
run;
Can you please post code with sample data which clearly illustrates the issue you're observing?
Thank you, Patrick. Your suggestion was helpful.
However, I modified the code a little more to ensure variables in each class are within the specified time period.
I made use of:
data xx;
set x;
Startdate1 = '15May2011'd - 30;
Enddate1 = '15May2011'd;
Startdate2 = '16May2011'd;
Enddate2 = '16May2011'd + 30;
If Startdate1 < date < Enddate1 then flag=0;
If Startdate2 < date < Enddate2 then flag=1;
run;
This worked fine for me. I'm still learning most of these little manipulations. Thank you for your help once more!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.