DATA Step, Macro, Functions and more

sas weeks

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

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.

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


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

View solution in original post


All Replies
Respected Advisor
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

Posted in reply to KurtBremser

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

Posted in reply to KurtBremser
Thanks a lot,
SS
Frequent Contributor
Posts: 76

Re: sas weeks

Posted in reply to KurtBremser

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

Posted in reply to KurtBremser

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

Posted in reply to SuryaKiran
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.

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 184 views
  • 1 like
  • 6 in conversation