DATA Step, Macro, Functions and more

How to Bring Current Week Number of the Year

Accepted Solution Solved
Reply
Super Contributor
Posts: 381
Accepted Solution

How to Bring Current Week Number of the Year

[ Edited ]

 

 

Hello,

 

First, I want to bring the last day of the month as format of YYYYMMDD (Example : 20151130) but it should also be previous month of the current date. After that, I need to bring the week number of the year. To Illustrate; I should see the macro as ‘201545’,’201546’,201547’,’201548’. Is there anyone who can help me write the macros which I mentioned (above).

 

Here is my tries;

 

%let P1_EOM_YYYYMMDD      = %substr(%sysfunc(intnx(month,&today,-1,end),yymmddn8.),1,8);

%put &P1_EOM_YYYYMMDD ;

 

http://support.sas.com/kb/24/619.html

 

My desired macro result should be ->

%put &LastMonth = 20151231

%put &WeeksOfLastMonth = ‘201549’,’20150’,201551’,’201552’,2015532'

 

Thank you

 


Accepted Solutions
Solution
‎01-26-2016 12:28 PM
Super User
Posts: 6,927

Re: How to Bring Current Week Number of the Year

[ Edited ]
data _null_;
week = .;
length outvar $100;
start = intnx('month',date(),-1,'begin');
end = intnx('month',date(),-1,'end');
do date = start to end;
  if week(date) ne week then do;
    outvar = catx(',',trim(outvar),"'"!!put(year(date),z4.)!!put(week(date),z2.)!!"'");
    week = week(date);
  end;
end;
call symput('WeeksOfLastMonth',trim(outvar));
call symput('LastMonth',put(end,yymmddn8.));
run;
%put &LastMonth;
%put &WeeksOfLastMonth;

See how I only used the data step language, as it is much better suited to doing calculations than the macro language, which is (as RW9 already mentioned) just a text generator.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: How to Bring Current Week Number of the Year

Question 1, as always, why are you doing it in a macro.  Macro language is a Text Replacement, or Codce Generation toolkit.  It isn't a Base SAS replacement.  If you need to keep parameters, keep them in datasets so you can, via the use of Base SAS, manipulate them as data elements:

data want;
  current_date=input("20151130",yymmdd8.);
  year=year(current_date);
  week=week(current_date);
  /* If needed then call that out here */
  call symput('P1_EOM',cats(year,week));
  format current_date date9.;
run;

%put &P1_EOM.;

See how keeping it Base SAS simplifies the calls, keeps the code readable, uses functions.  If you keep *data* in a dataset, then you can do all the mapping you want, and if you really have to have that in a macro then call symput it only at the point you use that.

Solution
‎01-26-2016 12:28 PM
Super User
Posts: 6,927

Re: How to Bring Current Week Number of the Year

[ Edited ]
data _null_;
week = .;
length outvar $100;
start = intnx('month',date(),-1,'begin');
end = intnx('month',date(),-1,'end');
do date = start to end;
  if week(date) ne week then do;
    outvar = catx(',',trim(outvar),"'"!!put(year(date),z4.)!!put(week(date),z2.)!!"'");
    week = week(date);
  end;
end;
call symput('WeeksOfLastMonth',trim(outvar));
call symput('LastMonth',put(end,yymmddn8.));
run;
%put &LastMonth;
%put &WeeksOfLastMonth;

See how I only used the data step language, as it is much better suited to doing calculations than the macro language, which is (as RW9 already mentioned) just a text generator.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 381

Re: How to Bring Current Week Number of the Year

Hello @KurtBremser ,

 

Your code is what I was trying to create. Thank you very much. When I execute your code in my environment, I don't get any error but the data set is created as empty. I think, I need to the change the mark as (") instead of('). I mean I need to see results as "201548","201549","201550","201551","201552" in the log. In the following statement I can't change the quotes because of the marks(" ' ") Do you have an idea ?

 

 outvar = catx(',',trim(outvar),"'"!!put(year(date),z4.)!!put(week(date),z2.)!!"'");

 @RW9 and @KurtBremser,Coming back to the your Macro question, I need to use the code more than one location in the SAS code.I thought If I create macro code I can call it whereever I want. I'd like to share that my program consists of a long code so that's why I need macro codes. But maybe I couldn't get your advises literally, If so, sorry for that. 

 

I'll be waiting your thoughts

 

Thank you.

Super User
Posts: 6,927

Re: How to Bring Current Week Number of the Year

[ Edited ]

You can always wrap a piece of SAS code into a macro so you avoid typing the same sequence repeatedly. But there's a difference between wrapping an easy-to-understand data step into a macro or creating a macro that is unnecessary complex, because it tries to do things in macro language that are easier done in data step language.

Another method would be to save the data step into a little .sas file and %include that wherever you need the code.

 

If you need the weeks enclosed in double quotes, then just replace the "'" (single quote enclosed by double quotes) in my code with '"' (double quote enclosed in single quotes)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 381

Re: How to Bring Current Week Number of the Year

@KurtBremser, I fixed the quote issue thank you for your help. I think I understood much better the macro discussion but I have to do more practice.

 

Thanks.

Super User
Posts: 6,927

Re: How to Bring Current Week Number of the Year

Before choosing a tool, contemplate what you want to achieve.

If you need to determine and set values for later use in a program, and you need data step functions for this, you are usually better off doing it in a data step and setting macro vars with call symput.

The macro language is basically designed to help you in creating code dynamically, not so much for doing calculations. The ability of the macro language to interface with data step functions is a helper for certain situations, but not its main purpose.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 9,662

Re: How to Bring Current Week Number of the Year


%macro xx;
%let LastMonth=%sysfunc(intnx(year,%sysfunc(today()),-1,end),yymmddn8.);

%let end=%sysfunc(intnx(year,%sysfunc(today()),-1,end));
%let StartWeek=%sysfunc(week(%sysfunc(intnx(month,&end,0,b))));
%let EndWeek=%sysfunc(week(&end));
%let year=%sysfunc(year(&end));

%let want_week=%sysfunc(quote(&year&StartWeek));
%do i=&StartWeek+1 %to &EndWeek;
 %let want_week=&want_week,%sysfunc(quote(&year&i));
%end;

%put &LastMonth &want_week ;
%mend xx;

%xx
Super Contributor
Posts: 381

Re: How to Bring Current Week Number of the Year

Thank you all fro your help Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 341 views
  • 5 likes
  • 4 in conversation