BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

I have a set of macro variables that I generated statically that I need to generate dynamically with a number of years parameter.  The macro variable needs to have quotes on both sides and no leading spaces.  An easy way to do this might be without making a macro loop.  

 

The text that I want is:

("201110", "201210", "201310")

when the parameter is 3

 

("201110", "201210")

when the parameter is 2

 

The closest I have so far is the script below.

 

%global academicPeriods;

%let fAcadP=201010;
%let fAcadYS=2009;

%let academicPeriods = %str();

%macro Parameters(n);
	%let academicPeriods = %str();
	%do i=1 %to &n - 1;
		%put &academicPeriods;
			%let academicPeriods = %sysfunc(cat(&academicPeriods, %eval(&fAcadP. +&i*100)", "));
	%end;
	%let academicPeriods =&academicPeriods %eval(&fAcadP +&n*100); 
%mend;
%Parameters(3);
%put(&academicPeriods);
1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Rhodochrosite | Level 12

@DavidPhillips2 

Here is my implementation

%macro Parameters(p_inStart=, p_count=, p_rtrnMacVarName=);
	%local 
		l_inStart
		l_i 
		l_dt;

	/* Generate space delimited Annual intervals */
	%let l_inStart = %sysfunc(inputn(&p_inStart,yymmn6.));
	%do l_i=1 %to &p_count;
		%let l_dt = %sysfunc(intnx(year,&l_inStart,&l_i,same));
		%let &p_rtrnMacVarName = &&&p_rtrnMacVarName %sysfunc(putn(&l_dt,yymmn6.));
	%end;
	
	/* Add sorounding double quotes ("") and delimit by comma (,) */
	%let &p_rtrnMacVarName =%str(%")%qsysfunc(tranwrd(&&&p_rtrnMacVarName,%str( ),%str(%",%") ))%str(%");
%mend;

%global academicPeriods;
%let academicPeriods = ;
%Parameters(p_inStart=201010, p_count=3, p_rtrnMacVarName=academicPeriods);
%put &=academicPeriods;
%put (&academicPeriods);
%put &=academicPeriods;
ACADEMICPERIODS="201110","201210","201310"
%put (&academicPeriods);
("201110","201210","201310")

This code avoids using global macro variable, only parameters passed into it. The starting date is a YYYYMM parameter, the interval count is a parameter, and a single line to quote the intervals and delimit by comma.

 

Hope this helps,

Ahmed

View solution in original post

11 REPLIES 11
Kurt_Bremser
Super User
%macro academicperiods(number);
data _null_;
length result $32767;
year = 2010;
do year = 1 to &number.;
  result = catx(",",result,quote(put(year,4.)!!"10"));
end;
result = cats("(",result,")"));
calo symputx("academicperiods",result,"g");
run;
%mend;
%academicperiods(2)
  
SASJedi
SAS Super FREQ

How about this?

%macro Parameters(n);
	%let academicPeriods = "%eval(&fAcadP. +&i*100)";
	%do i=2 %to &n - 1;
		%let academicPeriods = &academicPeriods ,"%eval(&fAcadP. +&i*100)";
	%end;
	%let academicPeriods =&academicPeriods, "%eval(&fAcadP +&n*100)"; 
%mend;
%Parameters(3);
%put NOTE: %superq(academicPeriods);
Check out my Jedi SAS Tricks for SAS Users
DavidPhillips2
Rhodochrosite | Level 12

Mark, thanks for replying to my thread.

 

I should have included the above let statements above:

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
201010 +&i*100
ERROR: The macro PARAMETERS will stop executing.

%global academicPeriods fAcadP;

%let fAcadP=201010;

%macro Parameters(n);
	%let academicPeriods = "%eval(&fAcadP. +&i*100)";
	%do i=2 %to &n - 1;
		%let academicPeriods = &academicPeriods ,"%eval(&fAcadP. +&i*100)";
	%end;
	%let academicPeriods =&academicPeriods, "%eval(&fAcadP +&n*100)"; 
%mend;
%Parameters(3);

PaigeMiller
Diamond | Level 26

It is almost never helpful to show us the error messages detached from the code. It is always helpful to show us the entire log for the run of your macro.

 

Since you are dealing with a macro, please turn on the macro debugging option by running this command.

 

options mprint;

 

Then run your macro and show us the log. Please show us the log by copying it as text and pasting it into the window that appears when you click on the </> icon.

PaigeMiller_0-1699900743276.png

--
Paige Miller
DavidPhillips2
Rhodochrosite | Level 12

 

1                                                          The SAS System                            11:56 Monday, February 19, 2024

1          ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='Program';
4          %LET _CLIENTPROCESSFLOWNAME='Process Flow';
5          %LET _CLIENTPROJECTPATH='';
6          %LET _CLIENTPROJECTPATHHOST='';
7          %LET _CLIENTPROJECTNAME='';
8          %LET _SASPROGRAMFILE='';
9          %LET _SASPROGRAMFILEHOST='';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=PNG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         FILENAME EGSR TEMP;
15         ODS tagsets.sasreport13(ID=EGSR) FILE=EGSR
16             STYLE=HtmlBlue
17             STYLESHEET=(URL="file:///C:/Program%20Files/SAS7Home/SASEnterpriseGuide/7.1/Styles/HtmlBlue.css")
18             NOGTITLE
19             NOGFOOTNOTE
20             GPATH=&sasworklocation
21             ENCODING=UTF8
22             options(rolap="on")
23         ;
NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
24         
25         GOPTIONS ACCESSIBLE;
26         options mprint;
27         %global academicPeriods fAcadP;
28         
29         %let fAcadP=201010;
30         
31         %macro Parameters(n);
32         	%let academicPeriods = "%eval(&fAcadP. +&i*100)";
33         	%do i=2 %to &n - 1;
34         		%let academicPeriods = &academicPeriods ,"%eval(&fAcadP. +&i*100)";
35         	%end;
36         	%let academicPeriods =&academicPeriods, "%eval(&fAcadP +&n*100)";
37         %mend;
38         %Parameters(3);
WARNING: Apparent symbolic reference I not resolved.
WARNING: Apparent symbolic reference I not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       201010 +&i*100 
ERROR: The macro PARAMETERS will stop executing.
39         
40         GOPTIONS NOACCESSIBLE;
41         %LET _CLIENTTASKLABEL=;
42         %LET _CLIENTPROCESSFLOWNAME=;
43         %LET _CLIENTPROJECTPATH=;
44         %LET _CLIENTPROJECTPATHHOST=;
45         %LET _CLIENTPROJECTNAME=;
46         %LET _SASPROGRAMFILE=;
47         %LET _SASPROGRAMFILEHOST=;
48         
49         ;*';*";*/;quit;run;
50         ODS _ALL_ CLOSE;
51         
52         
2                                                          The SAS System                            11:56 Monday, February 19, 2024

53         QUIT; RUN;
54         
options mprint; %global academicPeriods fAcadP; %let fAcadP=201010; %macro Parameters(n); %let academicPeriods = "%eval(&fAcadP. +&i*100)"; %do i=2 %to &n - 1; %let academicPeriods = &academicPeriods ,"%eval(&fAcadP. +&i*100)"; %end; %let academicPeriods =&academicPeriods, "%eval(&fAcadP +&n*100)"; %mend; %Parameters(3);

 

PaigeMiller
Diamond | Level 26
31         %macro Parameters(n);
32         	%let academicPeriods = "%eval(&fAcadP. +&i*100)";

&i does not have a value

--
Paige Miller
Tom
Super User Tom
Super User

That is easy to see what is happening:

Look at these two lines:

32         	%let academicPeriods = "%eval(&fAcadP. +&i*100)";
33         	%do i=2 %to &n - 1;

In line 32 you reference &I, but you don't give I a value until line 33.

AhmedAl_Attar
Rhodochrosite | Level 12

@DavidPhillips2 

Here is my implementation

%macro Parameters(p_inStart=, p_count=, p_rtrnMacVarName=);
	%local 
		l_inStart
		l_i 
		l_dt;

	/* Generate space delimited Annual intervals */
	%let l_inStart = %sysfunc(inputn(&p_inStart,yymmn6.));
	%do l_i=1 %to &p_count;
		%let l_dt = %sysfunc(intnx(year,&l_inStart,&l_i,same));
		%let &p_rtrnMacVarName = &&&p_rtrnMacVarName %sysfunc(putn(&l_dt,yymmn6.));
	%end;
	
	/* Add sorounding double quotes ("") and delimit by comma (,) */
	%let &p_rtrnMacVarName =%str(%")%qsysfunc(tranwrd(&&&p_rtrnMacVarName,%str( ),%str(%",%") ))%str(%");
%mend;

%global academicPeriods;
%let academicPeriods = ;
%Parameters(p_inStart=201010, p_count=3, p_rtrnMacVarName=academicPeriods);
%put &=academicPeriods;
%put (&academicPeriods);
%put &=academicPeriods;
ACADEMICPERIODS="201110","201210","201310"
%put (&academicPeriods);
("201110","201210","201310")

This code avoids using global macro variable, only parameters passed into it. The starting date is a YYYYMM parameter, the interval count is a parameter, and a single line to quote the intervals and delimit by comma.

 

Hope this helps,

Ahmed

DavidPhillips2
Rhodochrosite | Level 12

I should be able to convert the code block you posted to what I need.  It looks like i needed to use %str(%") and tranward.

Tom
Super User Tom
Super User

Why are you trying to use the SAS function CAT() in macro code?  There is not utility in doing that.  To concatenate values in macro code just place them next to each other.  Plus the CAT() series of functsion does not play well with %SYSFUNC() because they can take either character values or numeric values so poor %SYSFUNC() has to guess which type it should tell the CAT() function your macro expressions (which are always strings) are.

Start your %DO loop from ZERO instead of 1.

%let fAcadP=201010;
%macro Parameters(n);
  %local i sep;
(%do i=0 %to &n - 1;&sep."%eval(&facadp+100&i)"%let sep=,;%end;)
%mend;
%let academicPeriods = %Parameters(3);
%put &=academicperiods;

Or do the task in actual SAS code.

%let fAcadP=201010;
%let n=3;
data _null_;
  length result $3000;
  do i=0 to &n-1;
    result=catx(',',result,quote(put(&fAcadP+100*i,6.)));
  end;
  call symputx('academicperiods',cats('(',result,')'),'g');
run;
%put &=academicperiods;
DavidPhillips2
Rhodochrosite | Level 12

Thanks for posting the data step code.  That is much cleaner.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 11 replies
  • 1179 views
  • 4 likes
  • 6 in conversation