BookmarkSubscribeRSS Feed
Sfzz
Fluorite | Level 6

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!

 

5 REPLIES 5
ballardw
Super User

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.

Sfzz
Fluorite | Level 6

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

novinosrin
Tourmaline | Level 20

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;

Astounding
PROC Star

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.

Reeza
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2457 views
  • 4 likes
  • 5 in conversation