Solved
Contributor
Posts: 33

# 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.

However, I modified the code a little more to ensure variables in each class are within the specified time period.

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!

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.

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.

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

Thank you,

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.

However, I modified the code a little more to ensure variables in each class are within the specified time period.

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.