BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

What is wrong with this code that create two macro variables.

Error is related to DO that can not be done in open code

%let YYMMDD1=200421;
%let YYMMDD2='200420;
%macro mmacro;
%do i=1 %TO  2;
Data _null_;
Y&i=input(SUBSTR(COMPRESS(&&YYMMDD&i.),1,2),2.);
M&i.=input(SUBSTR(COMPRESS(&&YYMMDD&i.),3,2),2.);
D&i.=input(SUBSTR(COMPRESS(&&YYMMDD&i.),5,2),2.);
date&i.=MDY(M&i.,D&i.,Y&i.);
Run;
%put &date&i.;
%end;
%mend mmacro;
%mmacro;
4 REPLIES 4
ed_sas_member
Meteorite | Level 14

Hi @Ronein 

 

It seems that there is a missing quote line 2.

You also need to remove an ampersand at the beginning of &date&i, as date is not defined as a macrovariable in your code -> your code creates to variables, date1 and date2.

Best,

%let YYMMDD1=200421;
%let YYMMDD2=200420;

%macro mmacro;
	%do i=1 %to 2;
		data _null_;
			call symputx("date&i.", input("&&YYMMDD&i", YYMMDD6.));
		run;
		%put &&date&i.;
	%end;
%mend mmacro;

%mmacro;

&&date&i.  = &date1. if i=1

 

Jagadishkatam
Amethyst | Level 16

Please try the below code

 

in %put you should use &&date&i however you used &date&i

also if you want to create the macro variables as &date1 and &date2, we need to use the call symputx in the datastep which i have done in the code below.

 

%let YYMMDD1=200421;
%let YYMMDD2=200420;

%macro mmacro;
%do i=1 %TO  2;
Data _null_;
Y&i=input(SUBSTR(COMPRESS(&&YYMMDD&i.),1,2),best.);
M&i.=input(SUBSTR(COMPRESS(&&YYMMDD&i.),3,2),best.);
D&i.=input(SUBSTR(COMPRESS(&&YYMMDD&i.),5,2),best.);
call symputx("date&i.",MDY(M&i.,D&i.,Y&i.));
Run;

%put &&date&i.;
%end;

%mend mmacro;

%mmacro;

 

 

 

Thanks,
Jag
PaigeMiller
Diamond | Level 26

INPUT(SUBSTR(COMPRESS()) seems like an awful lot of work to enable you to work with dates. You should work with dates in macro variables as dates using date functions and date formats and date informats, rather than as a string to pull apart and recombine where the first two characters are YY and the next two characters are MM, etc.

 

%let yymmdd1=200421;
%let yymmdd2=200420;

%macro mmacro;
%do i=1 %TO  2;
data _null_;
file log;
date&i=input("&&yymmdd&i",yymmdd6.);
format date&i yymmdd.;
put date&i;
run;
%end;
%mend mmacro;
%mmacro

 or even simpler

 

%macro mmacro;
%do i=1 %TO  2;
%let date&i = %sysfunc(inputn(&&yymmdd&i,yymmdd6.));
%put date&i %sysfunc(putn(&&date&i,yymmdd.));
%end;
%mend mmacro;
%mmacro
--
Paige Miller
RichardDeVen
Barite | Level 11

The shown code 

%let YYMMDD2='200420;

is just incorrect and will put your SAS session into a open parsing state because of the lone single quote.

 

Presume for sake of argument, the initial macro variables could contain garbage characters that are not part of a proper yymmdd representation of a date.

 

You really don't need macro looping for this.  Your coding sanity is far safer using other statements.  I will presume the second macro assignment actually has a stray single quote.

 

So, on the premise that the digits within the macro value are date representation in the construct of  yymmdd, the INPUTN() function can parse those digits in to a SAS date value.  COMPRESS can process a string and keep only the digits.  You might want to convert the date value to it's date literal representation (maybe if you are writing a source code file for review and later submittal), or to a representation for reporting (such as a TITLE that has to show dd-MON-yyyy)

 

Example

options nosource;

%let YYMMDD1=200421;
%let YYMMDD2=%str(%'200420);

%put NOTE: YYMMDD1=%superq(YYMMDD1); 
%put NOTE: YYMMDD2=%superq(YYMMDD2);  %put --;

%let DATEVAL1 = %sysfunc(INPUTN(%sysfunc(COMPRESS(%superq(YYMMDD1),,KD)),YYMMDD8.));
%let DATEVAL2 = %sysfunc(INPUTN(%sysfunc(COMPRESS(%superq(YYMMDD2),,KD)),YYMMDD8.));

%put NOTE: &=DATEVAL1;
%put NOTE: &=DATEVAL2;   %put --;

%let DATE_LITERAL1 = "%sysfunc(PUTN(&DATEVAL1,DATE11.))"D;
%let DATE_LITERAL2 = "%sysfunc(PUTN(&DATEVAL2,DATE11.))"D;

%put NOTE: &=DATE_LITERAL1;
%put NOTE: &=DATE_LITERAL2;  %put --;

%let DATE1_FOR_TITLE = %sysfunc(PUTN(&DATEVAL1,DATE11.));
%let DATE2_FOR_TITLE = %sysfunc(PUTN(&DATEVAL2,DATE11.));

%put NOTE: &=DATE1_FOR_TITLE;
%put NOTE: &=DATE1_FOR_TITLE;  %put --;

options source;

Log

NOTE: YYMMDD1=200421
NOTE: YYMMDD2='200420
--
NOTE: DATEVAL1=22026
NOTE: DATEVAL2=22025
--
NOTE: DATE_LITERAL1="21-APR-2020"D
NOTE: DATE_LITERAL2="20-APR-2020"D
--
NOTE: DATE1_FOR_TITLE=21-APR-2020
NOTE: DATE1_FOR_TITLE=21-APR-2020
--

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 655 views
  • 0 likes
  • 5 in conversation