- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello everyone,
From a macro variable declared by a user (year), i want to have the first day of all the month of the period.
For example if someone put : %let year = 2014 2015; I want to have "01jan2014", "01feb2014", "01mar2014" ... "01nov2015" "01dec2015". I want to have this date save in different macro variable (date_1, date_2, date_3 ..., date_23, date_24).
So I have made a code, which works by half. I succeed in having the "01mmm" but not the YYYY.
%global year;
%let year = 2014 2015 ; /* the user choose the year*/
%global nb_year;
%let nb_year=%sysfunc(countw(&year.));
%put &nb_year. ; /*count the number of years*/
/* dates are saving in date_k (date_1 for "01jan2014", date_2 for '01feb2014"d ...) */
data _null_;
k=0;
ATTRIB month LENGTH=$5 ;
do i=1 to (&nb_year.);
year2 = scan(&year.,i,' ') ; /*this step doesn't work*/
do j=1 to 12 ;
if j=1 then month= "01jan";
if j=2 then month="01feb";
if j=3 then month="01mar";
if j=4 then month="01apr";
if j=5 then month="01may";
if j=6 then month="01jun";
if j=7 then month="01jul";
if j=8 then month="01aug";
if j=9 then month="01sep";
if j=10 then month="01oct";
if j=11 then month="01nov";
if j=12 then month="01dec" ;
k=k+1;
ym=(month)||(year2);
call symput(cats('date_',k),ym);
end;
end ;
run ;
%put &date_20.;
So everything works except the lign where i declare "year2".
Can somenone help me to solve my problem ? Sorry for my poor english ...
Best regards,
Alison
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, I really don't believe macro language is the best fit for data processing. Its a fundamental misunderstanding of what macro code is for. Macro is a text generation tool - it does nothing on its own, it has no data structures, it does not recognise dates or do calculation. I would do:
%let year = 2014 2015; data elements; do i=1 to countw("&year."," "); year=input(scan("&year.",i," "),best.); do j=1 to 12; date=mdy(j,1,year); output; end; end; format date date9.; run;
You will see how simple the code is, just a couple of loops - this is the power of Base SAS using proper datatypes. You can then use this dataset anyway you like - for instance in() lists, merging and such like. And if you really need macro variables use call symput(), however I would strongly advise against it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, I really don't believe macro language is the best fit for data processing. Its a fundamental misunderstanding of what macro code is for. Macro is a text generation tool - it does nothing on its own, it has no data structures, it does not recognise dates or do calculation. I would do:
%let year = 2014 2015; data elements; do i=1 to countw("&year."," "); year=input(scan("&year.",i," "),best.); do j=1 to 12; date=mdy(j,1,year); output; end; end; format date date9.; run;
You will see how simple the code is, just a couple of loops - this is the power of Base SAS using proper datatypes. You can then use this dataset anyway you like - for instance in() lists, merging and such like. And if you really need macro variables use call symput(), however I would strongly advise against it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much for your answer and your advice. It works very well 😉
Best regards.
Alison
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It should be:
Year2 = scan("&year", i);
But you'd be better off using SAS dates.
Do j=1 to 12;
Date= mdy(j, 1, year2);k+1;
Call symputx('date_'||k, put(date, date9.));
End;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, thank you it works too. I didn't know that we have to put " " around the macro-variable in a scan. Thank you ! Have a nice day 😉
Alison