SAS Programming

DATA Step, Macro, Functions and more
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 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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
Reeza
Super User

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.

arii
Obsidian | Level 7

Thank you, Reeza. This worked out well.

novinosrin
Tourmaline | Level 20

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

Thank you,  Novinosrin. This was helpful too!

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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