## need help in date fuction

Solved
Super Contributor
Posts: 271

# need help in date fuction

can we get the dates of all sunday for given year??????

Accepted Solutions
Solution
‎01-23-2013 06:23 AM
Posts: 1,147

## Re: need help in date fuction

Hi Keith,

A very good code to get all the dates in a year. very well executed. Appreciable.

@Aman4sas,

Keith has given a very good code, just make few changes to the code and it will work the way you want. As per your latest post, you wanted to pull any weekday from the given year. i have made few minor changes to Keith code. like i added an additional macro variable weekday, where you can give the number of the weekday for which you want to get the date (like 1 for sunday,2 for monday etc.,) and changes the do loop to list the days in a year. here i used 2 to retrieve the dates of weekday monday (2).

%let rpt_yr=2012;

%let weekday=2;

data sundays_&rpt_yr.;

format sunday_date date9.;

do i=0 to 366;

sunday_date=intnx('day',"01Jan&rpt_yr."d,i);

if weekday(sunday_date)=&weekday. then output;

end;

drop i;

run;

Thanks,

Thanks,
Jag

All Replies
Valued Guide
Posts: 2,191

## Re: need help in date fuction

how would you like the result? - a list in the SASlog, a comma separated list in a macro variable, an output report or a SAS dataset?

Regular Contributor
Posts: 151

## Re: need help in date fuction

Here's a simple piece of code that creates a dataset of all Sundays in a year.  The 'week' interval in the INTNX function returns Sunday by default.

%let rpt_yr=2012;

data sundays_&rpt_yr.;

format sunday_date date9.;

do i=0 to 52;

sunday_date=intnx('week',"01Jan&rpt_yr."d,i);

if year(sunday_date)=&rpt_yr. then output;

end;

drop i;

run;

Super Contributor
Posts: 271

## Re: need help in date fuction

Thanks for ur help sir,, but i m looking for not specific sunday,, any of day of week, like it can b monday ,, i can be friday,, n i m looking result in list view, not together sperated by comma,

Solution
‎01-23-2013 06:23 AM
Posts: 1,147

## Re: need help in date fuction

Hi Keith,

A very good code to get all the dates in a year. very well executed. Appreciable.

@Aman4sas,

Keith has given a very good code, just make few changes to the code and it will work the way you want. As per your latest post, you wanted to pull any weekday from the given year. i have made few minor changes to Keith code. like i added an additional macro variable weekday, where you can give the number of the weekday for which you want to get the date (like 1 for sunday,2 for monday etc.,) and changes the do loop to list the days in a year. here i used 2 to retrieve the dates of weekday monday (2).

%let rpt_yr=2012;

%let weekday=2;

data sundays_&rpt_yr.;

format sunday_date date9.;

do i=0 to 366;

sunday_date=intnx('day',"01Jan&rpt_yr."d,i);

if weekday(sunday_date)=&weekday. then output;

end;

drop i;

run;

Thanks,

Thanks,
Jag
Super Contributor
Posts: 271

## Re: need help in date fuction

Hi , its nice logic,, n its not near by actually its exact result what i was looking for,,

thanks for ur help.

i beg ur pardon to ask one more ques,, that what is the use of .(dot) which u use after macro variable..as i never used . for that, and it does same work with using .(dot)

%let rpt_yr=2012;

%let weekday=2;

data sundays_&rpt_yr.;             -> . after rpt_yr

format sunday_date date9.;

do i=0 to 366;

sunday_date=intnx('day',"01Jan&rpt_yr."d,i);   -> . after rpt_yr

if weekday(sunday_date)=&weekday. then output;  -> . after weekday

end;

drop i;

run;

i m realy sorry if u thnk its silly que,, but i m realy confused abt it,. what is the logic behind it,

Posts: 1,147

## Re: need help in date fuction

Hi,

if you are using a single macro variable like &rpt_yr then there is no need for you to put any . after the macro variable when you use it. it will work with the .

but there will be scenarios when you will use macro variable before the name of the variable like for example, in those situations if you dont use the . after the macro variable, it will consider the entire name as macro variable and it will search for that macro variable, to avoid that we use . after macro variable.

data xname;

set sashelp.class;

run;

%let name=x;

proc print data=&name.name;

run;

Thanks

Thanks,
Jag
🔒 This topic is solved and locked.

Discussion stats
• 6 replies
• 276 views
• 3 likes
• 4 in conversation