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
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;
And the question is?
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.
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;
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.
data want;
do year=1978 to 2023;
sunday=nwkdom(1,1,1,year);
output;
end;
format sunday date9.;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.