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
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;
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.
What is the rule for finding the first week of your year?
Last weekend of the every march (week start on sunday ends on Saturday).
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;
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 |
@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.
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 |
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;
.
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;
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;
@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.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.