BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sathya66
Barite | Level 11

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.

WeekWeek StartWeek End
126/03/201701/04/2017
202/04/201708/04/2017
309/04/201715/04/2017
.  
.  
5218/03/201824/04/2018

 

Thanks,

SS

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

13 REPLIES 13
PaigeMiller
Diamond | Level 26

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
sathya66
Barite | Level 11

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

Kurt_Bremser
Super User

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;
sathya66
Barite | Level 11
Thanks a lot,
SS
sathya66
Barite | Level 11

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
Kurt_Bremser
Super User

@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.

sathya66
Barite | Level 11

sorry for this, I know I accepted the solution.

my output should be like this.

dateweekbeginend
24/03/20185218/03/201824/03/2018
22/03/20185218/03/201824/03/2018
19/03/20185218/03/201824/03/2018
16/03/20185111/03/201817/03/2018
15/03/20185111/03/201817/03/2018
15/03/20185111/03/201817/03/2018
15/03/20185111/03/201817/03/2018
14/03/20185111/03/201817/03/2018
14/03/20185111/03/201817/03/2018
13/03/20185111/03/201817/03/2018
10/03/20185004/03/201810/03/2018
10/03/20185004/03/201810/03/2018
09/03/20185004/03/201810/03/2018
09/03/20185004/03/201810/03/2018
09/03/20185004/03/201810/03/2018
SuryaKiran
Meteorite | Level 14

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
sathya66
Barite | Level 11
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
Ksharp
Super User
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;
sathya66
Barite | Level 11

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;
ballardw
Super User

@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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 13 replies
  • 2040 views
  • 1 like
  • 6 in conversation