BookmarkSubscribeRSS Feed
ybz12003
Rhodochrosite | Level 12

Good morning,

 

I have the following long codes, I am wondering if I could use a macro to simply this procedure.  Thanks in advance.

 

%let startdate_2016=01JUL2016; 
%let enddate_2016=30JUN2017; 
%let startdate_2015=01JUL2015;
%let enddate_2015=30JUN2016;
%let startdate_2014=01JUL2014;
%let enddate_2014=30JUN2015;
%let startdate_2013=01JUL2013;
%let enddate_2013=30JUN2014;
%let startdate_2012=01JUL2012;
%let enddate_2012=30JUN2013;
%let startdate_2011=01JUL2011; 
%let enddate_2011=30JUN2012; 
%let startdate_2010=01JUL2010;
%let enddate_2010=30JUN2011;
9 REPLIES 9
Tom
Super User Tom
Super User

Why do you need so many macro variables?

%let year=2016;
%let startdate=01JUL&year;
%let enddate=30JUN%eval(&year+1);

Anyway.

%do year=2010 %to 2016;
  %let startdate_&year=01JUL&year;
  %let enddate_&year=30JUN%eval(&year+1);
%end;
ybz12003
Rhodochrosite | Level 12

Hi Tom,

 

Got error message in Log.

 

15 %do year=2010 %to 2016;
ERROR: The %DO statement is not valid in open code.
16 %let startdate_&year=01JUL&year;
WARNING: Apparent symbolic reference YEAR not resolved.
ERROR: Symbolic variable name STARTDATE_&YEAR must contain only letters, digits, and underscores.
17 %let enddate_&year=30JUN%eval(&year+1);
WARNING: Apparent symbolic reference YEAR not resolved.
ERROR: Symbolic variable name ENDDATE_&YEAR must contain only letters, digits, and underscores.
18 %end;
ERROR: The %END statement is not valid in open code.

PaigeMiller
Diamond | Level 26

@ybz12003 wrote:

Hi Tom,

 

Got error message in Log.

 

15 %do year=2010 %to 2016;
ERROR: The %DO statement is not valid in open code.
16 %let startdate_&year=01JUL&year;
WARNING: Apparent symbolic reference YEAR not resolved.
ERROR: Symbolic variable name STARTDATE_&YEAR must contain only letters, digits, and underscores.
17 %let enddate_&year=30JUN%eval(&year+1);
WARNING: Apparent symbolic reference YEAR not resolved.
ERROR: Symbolic variable name ENDDATE_&YEAR must contain only letters, digits, and underscores.
18 %end;
ERROR: The %END statement is not valid in open code.


You have to run the %DO loop inside a macro. Or use the DATA step I provided.

--
Paige Miller
PaigeMiller
Diamond | Level 26

How about using a data step loop? 

 

data _null_;
    do start_year=2010 to 2016;
    call symputx(cats('startdate_',year),mdy(7,1,year));
    call symputx(cats('enddate_',year),mdy(6,30,year+1));
    end;
run;  

 

--
Paige Miller
ybz12003
Rhodochrosite | Level 12

Hi PaigeMiller,

 

Your codes are shown error too.

 

19 data _null_;
20 do start_year=2010 to 2016;
21 call symputx(cats('startdate_',year),mdy(7,1,year));
22 call symputx(cats('enddate_',year),mdy(6,30,year+1));
23 end;
24 run;

NOTE: Variable year is uninitialized.
ERROR: Symbolic variable name STARTDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('startdate_.',.) at line 21 column 10 is invalid.
ERROR: Symbolic variable name ENDDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('enddate_.',.) at line 22 column 10 is invalid.
ERROR: Symbolic variable name STARTDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('startdate_.',.) at line 21 column 10 is invalid.
ERROR: Symbolic variable name ENDDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('enddate_.',.) at line 22 column 10 is invalid.
ERROR: Symbolic variable name STARTDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('startdate_.',.) at line 21 column 10 is invalid.
ERROR: Symbolic variable name ENDDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('enddate_.',.) at line 22 column 10 is invalid.
ERROR: Symbolic variable name STARTDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('startdate_.',.) at line 21 column 10 is invalid.
ERROR: Symbolic variable name ENDDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('enddate_.',.) at line 22 column 10 is invalid.
ERROR: Symbolic variable name STARTDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('startdate_.',.) at line 21 column 10 is invalid.
ERROR: Symbolic variable name ENDDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('enddate_.',.) at line 22 column 10 is invalid.
ERROR: Symbolic variable name STARTDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('startdate_.',.) at line 21 column 10 is invalid.
ERROR: Symbolic variable name ENDDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('enddate_.',.) at line 22 column 10 is invalid.
ERROR: Symbolic variable name STARTDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('startdate_.',.) at line 21 column 10 is invalid.
ERROR: Symbolic variable name ENDDATE_. must contain only letters, digits, and underscores.
NOTE: Argument 1 to function SYMPUTX('enddate_.',.) at line 22 column 10 is invalid.
start_year=2017 year=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
7 at -22:-53 7 at 21:42 7 at 22:40
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.31 seconds
cpu time 0.21 seconds

Tom
Super User Tom
Super User

You can fix that yourself. The log is even giving you hint if you are having trouble seeing it.

NOTE: Variable year is uninitialized.
PaigeMiller
Diamond | Level 26

@ybz12003 wrote:

Hi PaigeMiller,

 

Your codes are shown error too.

 

19 data _null_;
20 do start_year=2010 to 2016;
21 call symputx(cats('startdate_',year),mdy(7,1,year));
22 call symputx(cats('enddate_',year),mdy(6,30,year+1));
23 end;
24 run;

NOTE: Variable year is uninitialized.


Yes, that was my mistake, which you should be able to fix easily

--
Paige Miller
ybz12003
Rhodochrosite | Level 12

Not from me, but those individual variables will be used in the next checking procedure.    

ballardw
Super User

@ybz12003 wrote:

Not from me, but those individual variables will be used in the next checking procedure.    


The specific list of values you show makes me suspect that someone does not know how to use the INTNX function to get the start/end value of a month.

 

I would say just seeing that many macro variables that the process is likely overly convoluted and possibly trying to force a spreadsheet solution onto SAS data by maintaining poor structures and using "loopy" code instead of By group processing.

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
  • 9 replies
  • 1252 views
  • 1 like
  • 4 in conversation