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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

alisondu77
Obsidian | Level 7

Thank you so much for your answer and your advice. It works very well 😉

 

Best regards.

 

Alison

Reeza
Super User

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;
alisondu77
Obsidian | Level 7

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1280 views
  • 7 likes
  • 3 in conversation