DATA Step, Macro, Functions and more

Weekday dummy Variables

Reply
New Contributor
Posts: 3

Weekday dummy Variables

I have dataset  which has around one million rows and It has Period as one column. I need to create 7 dummy variables, each for one day.

So is there any function which can create weekday dummy variables by looking at the period column?

 

Thanks in Advance!

 

Super User
Posts: 10,474

Re: Weekday dummy Variables

What is in that period variable? Is it a string value or a SAS date or something else? For a given period what rule is used to assign values to which dummy variable?

 

The answer is dependant on the above, there are likely any number of functions/ code steps to help but values are neede, input and desired output.

 

It may be time to run proc contents on your data set if you are not sure if your values are numeric or character and if values you think are "dates" actually have a SAS date, or time, or datetime, format associated.

New Contributor
Posts: 3

Re: Weekday dummy Variables

Yes, period is SAS Date. Im attaching sample in excel file.

PROC Star
Posts: 162

Re: Weekday dummy Variables

data have;

infile datalines truncover;

input period $30. ;

datalines;

Sunday, June 4, 2017

Monday, June 5, 2017

Tuesday, June 6, 2017

Wednesday, June 7, 2017

Thursday, June 8, 2017

Friday, June 9, 2017

Saturday, June 10, 2017

Sunday, June 11, 2017

Monday, June 12, 2017

Tuesday, June 13, 2017

Wednesday, June 14, 2017

Thursday, June 15, 2017

Friday, June 16, 2017

Saturday, June 17, 2017

Sunday, June 18, 2017

Monday, June 19, 2017

Tuesday, June 20, 2017

Wednesday, June 21, 2017

Thursday, June 22, 2017

Friday, June 23, 2017

Saturday, June 24, 2017

Sunday, June 25, 2017

Monday, June 26, 2017

Tuesday, June 27, 2017

Wednesday, June 28, 2017

Thursday, June 29, 2017

Friday, June 30, 2017

Saturday, July 1, 2017

Sunday, July 2, 2017

Monday, July 3, 2017

Tuesday, July 4, 2017

Wednesday, July 5, 2017

Thursday, July 6, 2017

Friday, July 7, 2017

Saturday, July 8, 2017

Sunday, July 9, 2017

Monday, July 10, 2017

Tuesday, July 11, 2017

Wednesday, July 12, 2017

Thursday, July 13, 2017

Friday, July 14, 2017

Saturday, July 15, 2017

;

 

data want;

set have;

array wkday(*)  Sunday      Monday     Tuesday    Wednesday       Thursday  Friday      Saturday (7*0);

do _n_=1 to dim(wkday);

if vname(wkday(_n_))=scan(period,1) then wkday(_n_)=1;

else wkday(_n_)=0;

end;

run;

Super User
Posts: 5,076

Re: Weekday dummy Variables

As long as you are starting with a SAS date, it's easy to create a variable that takes on values of 1 through 7, depending on the day of the week:

 

WD = weekday(period);

 

1 = Sunday, 7 = Saturday

 

While you could create dummy variables from these values, it's probably unnecessary.  SAS procedures that would use dummy variables usually let you specify WD as a CLASS variable, so the procedure can create its own dummy variables.

Super User
Posts: 17,771

Re: Weekday dummy Variables

Here's a variation on @novinosrin solution. 

 

Most important changes - no loop initialization as that seems to imply RETAIN. This assumes you're working with SAS dates, not character variables. 

 

*create sample data;
data have;
do period='04Jun2017'd to '15Jul2017'd;
output;
end;
format period weekdate21.;
run;

*create array/dummy vars - note this is overparameterized;
data want;
set have;
array wkday(*) Days1-Days7;

*initialize to 0, if initialized, implicit retain messes up data;
do i=1 to 7;
wkday(i)=0;
end;

*find day of week for period;
day_of_week = weekday(period);
*assign to value of 1;
wkday(day_of_week)=1;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 117 views
  • 4 likes
  • 5 in conversation