## sas weeks

Solved
Frequent Contributor
Posts: 76

# sas weeks

Hi all,

year will start from  26/03/2017 end 24/03/2018

start from  25/03/2018  end so on.

my output should be like this. I need this for every year within macro.

 Week Week Start Week End 1 26/03/2017 01/04/2017 2 02/04/2017 08/04/2017 3 09/04/2017 15/04/2017 . . 52 18/03/2018 24/04/2018

Thanks,

SS

Accepted Solutions
Solution
‎04-11-2018 09:26 AM
Super User
Posts: 10,278

## Re: sas weeks

Then try this:

``````%let year = 2017;

data weeks (keep=week begin end);
begindate = intnx('month',mdy(3,1,&year),0,'e');
begindate = begindate - weekday(begindate) + 1;
enddate = intnx('month',mdy(3,1,&year + 1),0,'e');
enddate = enddate - weekday(enddate);
put begindate enddate;
format
week 2.
begin
end
yymmddd10.
;
begin = begindate;
week = 1;
do until (begin > enddate);
end = begin + 6;
output;
week + 1;
begin = begin + 7;
put week begin end;
end;
run;
``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Posts: 3,055

## Re: sas weeks

If you have the start date (26/03/2017), and add 7 to that number, you get the end date of the week. If you add 1 to the end date of the previous week, you get the start date of the next week. And so on.

--
Paige Miller
Super User
Posts: 10,278

## Re: sas weeks

What is the rule for finding the first week of your year?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 76

## Re: sas weeks

Last weekend of the every march (week start on sunday ends on Saturday).

Solution
‎04-11-2018 09:26 AM
Super User
Posts: 10,278

## Re: sas weeks

Then try this:

``````%let year = 2017;

data weeks (keep=week begin end);
begindate = intnx('month',mdy(3,1,&year),0,'e');
begindate = begindate - weekday(begindate) + 1;
enddate = intnx('month',mdy(3,1,&year + 1),0,'e');
enddate = enddate - weekday(enddate);
put begindate enddate;
format
week 2.
begin
end
yymmddd10.
;
begin = begindate;
week = 1;
do until (begin > enddate);
end = begin + 6;
output;
week + 1;
begin = begin + 7;
put week begin end;
end;
run;
``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 76

## Re: sas weeks

Thanks a lot,
SS
Frequent Contributor
Posts: 76

## Re: sas weeks

sorry I am wrong,

my dates are like this. I should make that in  week, weekbegin,weekend.

 24/03/2018 22/03/2018 19/03/2018 16/03/2018 15/03/2018 15/03/2018 15/03/2018 14/03/2018 14/03/2018 13/03/2018 10/03/2018 10/03/2018 09/03/2018 09/03/2018
Super User
Posts: 10,278

## Re: sas weeks

[ Edited ]

@sathya66 wrote:

sorry I am wrong,

my dates are like this. I should make that in  week, weekbegin,weekend.

 24/03/2018 22/03/2018 19/03/2018 16/03/2018 15/03/2018 15/03/2018 15/03/2018 14/03/2018 14/03/2018 13/03/2018 10/03/2018 10/03/2018 09/03/2018 09/03/2018

Expand my data step to create a cntlin file for proc format, and use the formats:

``````%let year=2017;

data
weeks (keep=week begin end)
cntlin (keep=fmtname type start label)
;
begindate = intnx('month',mdy(3,1,&year),0,'e');
begindate = begindate - weekday(begindate) + 1;
enddate = intnx('month',mdy(3,1,&year + 1),0,'e');
enddate = enddate - weekday(enddate);
format
week 2.
begin
end
yymmddd10.
fmtname \$20.
;
begin = begindate;
week = 1;
do until (begin > enddate);
end = begin + 6;
output weeks;
type = 'N';
do start = begin to end;
label = week;
fmtname = "myweeks_&year._";
output cntlin;
label = begin;
fmtname = "begindate_&year._";
output cntlin;
label = end;
fmtname = "enddate_&year._";
output cntlin;
end;
week + 1;
begin = begin + 7;
end;
run;

proc sort data=cntlin;
by fmtname;
run;

proc format library=work cntlin=cntlin;
run;

data want;
input refdate ddmmyy10.;
week = input(put(refdate,myweeks_&year._.),best.);
begindate = input(put(refdate,begindate_&year._.),best.);
enddate = input(put(refdate,enddate_&year._.),best.);
format
refdate
begindate
enddate
ddmmyy10.
;
cards;
24/03/2018
22/03/2018
19/03/2018
16/03/2018
15/03/2018
15/03/2018
15/03/2018
14/03/2018
14/03/2018
13/03/2018
10/03/2018
10/03/2018
09/03/2018
09/03/2018
;
run;``````

Edit: removed an unnecessary diagnostic step.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 76

## Re: sas weeks

sorry for this, I know I accepted the solution.

my output should be like this.

 date week begin end 24/03/2018 52 18/03/2018 24/03/2018 22/03/2018 52 18/03/2018 24/03/2018 19/03/2018 52 18/03/2018 24/03/2018 16/03/2018 51 11/03/2018 17/03/2018 15/03/2018 51 11/03/2018 17/03/2018 15/03/2018 51 11/03/2018 17/03/2018 15/03/2018 51 11/03/2018 17/03/2018 14/03/2018 51 11/03/2018 17/03/2018 14/03/2018 51 11/03/2018 17/03/2018 13/03/2018 51 11/03/2018 17/03/2018 10/03/2018 50 04/03/2018 10/03/2018 10/03/2018 50 04/03/2018 10/03/2018 09/03/2018 50 04/03/2018 10/03/2018 09/03/2018 50 04/03/2018 10/03/2018 09/03/2018 50 04/03/2018 10/03/2018
Valued Guide
Posts: 597

## Re: sas weeks

You can try something like this and if you want them in macro variables then use

``````%let start_Date=26MAR2017;
%let End_Date=24MAR2018;

Data want(Drop=i) ;
Format Start_Date End_Date Date9.;
Week=0;
Do i="&Start_Date"D to  "&End_Date"D by  7;
Start_Date=i;
End_Date=i+6;
Week=Week+1;
output;
end;
run;``````

.

Thanks,
Suryakiran
Frequent Contributor
Posts: 76

## Re: sas weeks

Thanks Surya,
It is working but start date and end dates should be dynamic, As I said start date/week is weekend of March.(Sunday to Saturday).
Thanks,
SS
Super User
Posts: 10,784

## Re: sas weeks

``````data have;
input date ddmmyy12.;
week=week(date-84);
start=intnx('week',date,0,'b');
end=intnx('week',date,0,'e');

format date start end ddmmyy10.;
cards;
24/03/2018
22/03/2018
19/03/2018
16/03/2018
15/03/2018
15/03/2018
15/03/2018
14/03/2018
14/03/2018
13/03/2018
10/03/2018
10/03/2018
09/03/2018
09/03/2018
;
run;``````
Frequent Contributor
Posts: 76

## Re: sas weeks

It is not working for this data.

``````data have;
input date ddmmyy12.;
week=week(date-84);
start=intnx('week',date,0,'b');
end=intnx('week',date,0,'e');

format date start end ddmmyy10.;
cards;
26/03/2016
27/03/2017
28/03/2017
24/03/2017
22/03/2016
19/03/2017
16/03/2016
15/03/2017
15/03/2017
15/03/2017
14/03/2017
14/03/2017
13/03/2017
10/03/2017
10/03/2017
09/03/2017
09/03/2017
;
run;``````
Super User
Posts: 13,583

## Re: sas weeks

@sathya66 wrote:

It is not working for this data.

``````data have;
input date ddmmyy12.;
week=week(date-84);
start=intnx('week',date,0,'b');
end=intnx('week',date,0,'e');

format date start end ddmmyy10.;
cards;
26/03/2016
27/03/2017
28/03/2017
24/03/2017
22/03/2016
19/03/2017
16/03/2016
15/03/2017
15/03/2017
15/03/2017
14/03/2017
14/03/2017
13/03/2017
10/03/2017
10/03/2017
09/03/2017
09/03/2017
;
run;``````

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.