Creating a 'Class Variable' for Observations in a Specified Time Period.

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Creating a 'Class Variable' for Observations in a Specified Time Period.

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!


Accepted Solutions
Solution
‎03-27-2018 12:03 AM
Contributor
Posts: 33

Re: Creating a 'Class Variable' for Observations in a Specified Time Period.

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


All Replies
Contributor
Posts: 33

Re: Creating a 'Class Variable' for Observations in a Specified Time Period.

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;

Valued Guide
Posts: 597

Re: Creating a 'Class Variable' for Observations in a Specified Time Period.

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
Contributor
Posts: 33

Re: Creating a 'Class Variable' for Observations in a Specified Time Period.

Posted in reply to SuryaKiran

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.

 

 

Valued Guide
Posts: 597

Re: Creating a 'Class Variable' for Observations in a Specified Time Period.

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
Contributor
Posts: 33

Re: Creating a 'Class Variable' for Observations in a Specified Time Period.

Posted in reply to SuryaKiran

Thank you, 

Respected Advisor
Posts: 4,737

Re: Creating a 'Class Variable' for Observations in a Specified Time Period.

@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?

Solution
‎03-27-2018 12:03 AM
Contributor
Posts: 33

Re: Creating a 'Class Variable' for Observations in a Specified Time Period.

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 170 views
  • 2 likes
  • 3 in conversation