BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
arii
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
arii
Obsidian | Level 7

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!

View solution in original post

7 REPLIES 7
arii
Obsidian | Level 7

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;

SuryaKiran
Meteorite | Level 14

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;

 

 

Thanks,
Suryakiran
arii
Obsidian | Level 7

Thank you, SuryaKiran.

 

See below as requested:

 

DateRSP
1/4/199322
2/5/199944
11/16/20035
1/7/20055
3/18/20065
1/11/200763
2/12/200892
5/13/201156
1/14/201293
5/15/20126
5/16/20124
5/19/20123
6/20/20122
9/21/201345

 

 

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.

 

 

SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran
arii
Obsidian | Level 7

Thank you, 

Patrick
Opal | Level 21

@arii

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?

arii
Obsidian | Level 7

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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 660 views
  • 2 likes
  • 3 in conversation