BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
data ds (drop=st  ed);
do date='01jan1970'd to '01jan2023'd  by 12 ;
if weekday(date)=1 then output;
end;
format date date9.;
w=put(i,downame.);
proc print ;
run;

Herer i am trying to get 1st Jan  comes in sundays for given date range

 

requiried output

1978, 1989, 1995, 2006, 2017, and 2023 years 

1st Jan comes in suday

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's one way to approach that.

 

data want;
  do year = 1970 to 2023;
      jan1 = mdy(1, 1, year);
      if weekday(jan1) = 1 then output;
   end;
   format jan1 date9.;
run;

View solution in original post

8 REPLIES 8
andreas_lds
Jade | Level 19

And the question is?

BrahmanandaRao
Lapis Lazuli | Level 10
I want get which years 1st Jan come sundays given date range
Kurt_Bremser
Super User

Dates are counts of days, not counts of months. That's why BY 12 can't work.

Instead of an iterative DO loop, use a DO WHILE and the INTNX function to increment.

Astounding
PROC Star

Here's one way to approach that.

 

data want;
  do year = 1970 to 2023;
      jan1 = mdy(1, 1, year);
      if weekday(jan1) = 1 then output;
   end;
   format jan1 date9.;
run;
BrahmanandaRao
Lapis Lazuli | Level 10
Thank you Astounding
your are solved this question
ballardw
Super User

Consider just using the date of interest, 1 Jan for each year.

data ds ;
do year=1970 to 2023;
  if weekday(mdy(1,1,year)) = 1 then output;
end;
run;

Your do loop by 12 was incrementing by 12 day intervals, not months or years so was pretty far off. And since 12 seldom goes into a year evenly when considering days doesn't hit a 1 Jan date very often.

The MDY function creates a date value from numeric parameters of Month number, day number and year.

Ksharp
Super User
data want;
 do year=1978 to 2023;
   sunday=nwkdom(1,1,1,year);
   output;
 end;
 format sunday date9.;
run;
BrahmanandaRao
Lapis Lazuli | Level 10
Thank you Sharp

data want;
do year = 1960 to 2050;
jan1 = mdy(1, 1, year);
if weekday(jan1) = 1 then output;
end;
format jan1 downame.;
proc print;
run;
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
  • 8 replies
  • 1728 views
  • 4 likes
  • 6 in conversation