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
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.
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.
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.
Hello @Kurt_Bremser ,
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 @Kurt_Bremser,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.
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)
@Kurt_Bremser, 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.
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.
%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
Thank you all fro your help 🙂
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: