BookmarkSubscribeRSS Feed
alilacey0
Fluorite | Level 6

 Using appropriate SAS DATE functions and maybe PROC FREQ, find a frequency table containing the number of Sundays (1), Mondays (2), ...., Saturdays (7) in the year 2017. You can enter your data/dates in any format you want.

8 REPLIES 8
mkeintz
PROC Star

Homework, eh?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Quentin
Super User

I think homework questions are fine.  But as with any other question, it's important for you to two show what code you have tried, and describe how your code failed (errors in the log? incorrect results?)  By posting your code, people can identify the error your made and help you understand the error (and correct it).  By posting your code, you help people help you.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
alilacey0
Fluorite | Level 6

this is what I have so far, but we are having troubles getting the days of the week for 2017

I attached some of our results as well

 

(this does not run)

 

data dates;
do i=1 to 365;
Sundays = intck('weekday', '01Jan2017'd, '31Dec2017'd);
day = weekday(week);
run;


title "Number of Each Day of the Week in 2017";
proc print data == dates noobs;
var week Day -- Year;
run;
 
(this does run but not able to get any further, results are attached)
data days;
do i=0 to 365;
Day = intck('week', '01Jan2017'd, '31Dec2017'd);
end;
run;
 
title "Listing of Days of the Week for 2017";
proc print data=days noob;
run;

 

error_prone
Barite | Level 11
Does the first data-step what you expected? There should be at least notes about missing values in the log.

The following untested code (send from my mobile phone) should create a dataset to start with:
data dates;
do date = "1Jan2017"d to "31Dec2017"d;
dow = weekday(date);
output;
end;
run;

Now have a look a the documentation of proc freq ... the task should be easy to solve.
ChrisBrooks
Ammonite | Level 13

In the interests of full disclosure (and not wanting to commit plagiarism) I found this answer on stackoverflow https://stackoverflow.com/questions/40676721/number-of-sundays-between-two-dates

 

data _null_;
	format a b date9.;
	a='01 nov 2016'd;
	b='18 nov 2016'd;
	Sundays = intck('weekday234567w',a,b);
	put _all_;
run;
mkeintz
PROC Star

Just note that when using the INTCK function to count a particular day-of-week, the "from" date should be the day before the actual start of interval.

 

Consider '03jan2016'd and '10jan2016'd which are both Sunday, so for that date range you would want N_SUN=2.  But

 

     start='03jan2016'd;

     end='10jan2016'd;

     n_sun=intck('week.1',start,end)  ==> 1   (not 2)

or identically
    n_sun=intck('weekday234567w',start,end) = 1

 

so use

   n_sun=intck('week.1',start-1,end)

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mkeintz
PROC Star

The INTCK('week',from_date,to_date) will count the number of saturday/sunday boundaries crossed between from_date through to_date.

 

So consider;

 

   n_sun2017=intck('week','31dec2016'd,'31dec2017'd);

or equivalently use the "week.1" interval descriptor.

   n_sun2017=intck('week.1','31dec2016'd,'31dec2017'd);

 

 

Number of Monday's?  Then you need to count the number of sun/mon boundaries, so use the "week.2" interval descriptor:

   n_mon2017=intck('week.2','31dec2016'd,'31dec2017'd);

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ShiroAmada
Lapis Lazuli | Level 10

Try this.....

data HAVE;
  start='01jan2016'd; end='31dec2016'd; output;
  start='01jan2017'd; end='31dec2017'd; output;
run;

data want;
  set have;
 sunday=0;
do i=start to end;
  sunday=ifn(weekday(i)=1, sum(sunday,1),sunday);
  retain sunday;
end;
run;

Hope this helps.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 4466 views
  • 4 likes
  • 6 in conversation