Create dummy variables by date range

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Create dummy variables by date range

[ Edited ]

Hi,

 

I am still struggling with some SAS codes, i would really appreciate some help here.

 

I need to create two dummy variables (d1 and d2) as follows:

 

d1 =  observations that are within 01/03/2001 - 30/11/2001, (otherwise = 0)

d2 = observations that are within 01/12/2007 - 30/06/2009, (otherwise = 0)

 

(Note: date format: mm/dd/year)

 

 

My new model would be (with the dummy variables):

 

Y = X0 + a1d1 + a2d2 + b1X1 + b2X2 + U

 

See my sample data below:

 

Date X1 X2
1/2/2001 22 78
2/5/2001 34 75
4/6/2001 65 65
5/7/2001 11 30
6/8/2001 56 58
10/11/2001 31 29
12/12/2001 91 56
1/13/2004 49 45
7/18/2007 56 41
9/19/2007 21 44
12/2/2007 67 12
12/21/2007 34 41
12/29/2007 67 70
1/23/2008 90 34
7/14/2008 78 65
9/25/2008 45 11
6/28/2009 45 56
1/29/2010 54 31

 

 

 

I tried this code (which is obviously not right):

 

Data DVs;

set ABC;

 

array monthdummy(*) 032001 - 062009;

do i=3/1/2001 to 11/30/2001;

do ii=12/1/2007 to 06/30/2009;

 

if datamonth = i then monthdummy{i} = 1;

if datamonth = ii then monthdummy{ii} = 2;

 

else monthdummy{i} = 0;

else monthdummy{ii} = 0;

run;

 

 

Please help.


Accepted Solutions
Solution
‎03-22-2018 04:08 PM
Super User
Posts: 23,776

Re: Create dummy variables by date range

Your dates are ddmmyy unless you have 30 months somehow?

 

d1 =  observations that are within 01/03/2001 - 30/11/2001, (otherwise = 0)

d2 = observations that are within 01/12/2007 - 30/06/2009, (otherwise = 0)

 

(Note: date format: mm/dd/year)

 

Here's an example of how this could be done. 

 

data want;
set have;

if '01Mar2001'd <= date <= '30Nov2001'd then d1=1;
else d1=0;

run;

You can specify date values as 'DDMONYYYY'd with the D at the end. It must be in the DATE9 format as I've demonstrated.

As long as your variable is a date, regardless of format shown, it should work. 
If it's a datetime or character you'll need to first convert it to a date variable.

View solution in original post


All Replies
Solution
‎03-22-2018 04:08 PM
Super User
Posts: 23,776

Re: Create dummy variables by date range

Your dates are ddmmyy unless you have 30 months somehow?

 

d1 =  observations that are within 01/03/2001 - 30/11/2001, (otherwise = 0)

d2 = observations that are within 01/12/2007 - 30/06/2009, (otherwise = 0)

 

(Note: date format: mm/dd/year)

 

Here's an example of how this could be done. 

 

data want;
set have;

if '01Mar2001'd <= date <= '30Nov2001'd then d1=1;
else d1=0;

run;

You can specify date values as 'DDMONYYYY'd with the D at the end. It must be in the DATE9 format as I've demonstrated.

As long as your variable is a date, regardless of format shown, it should work. 
If it's a datetime or character you'll need to first convert it to a date variable.

Contributor
Posts: 33

Re: Create dummy variables by date range

Thank you, Reeza. This worked out well.

PROC Star
Posts: 1,844

Re: Create dummy variables by date range

Or go binary

data have;
input Date :mmddyy10.	X1	X2;
format Date :mmddyy10.;
datalines;
1/2/2001	22	78
2/5/2001	34	75
4/6/2001	65	65
5/7/2001	11	30
6/8/2001	56	58
10/11/2001	31	29
12/12/2001	91	56
1/13/2004	49	45
7/18/2007	56	41
9/19/2007	21	44
12/2/2007	67	12
12/21/2007	34	41
12/29/2007	67	70
1/23/2008	90	34
7/14/2008	78	65
9/25/2008	45	11
6/28/2009	45	56
1/29/2010	54	31
;

data want;
set have;
d1='01Mar2001'd <= date <= '30Nov2001'd;
run;
Contributor
Posts: 33

Re: Create dummy variables by date range

Posted in reply to novinosrin

Thank you,  Novinosrin. This was helpful too!

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 164 views
  • 3 likes
  • 3 in conversation