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

The code below must do the same thing in three different ways using the DATA step, SAS/IML, and SAS Macro, respectively, but unfortunately the third one doesn't work and just produces some warning messages saying "WARNING: Argument 2 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range."

data _null_;
	do I=1 to 10;
		J=putn(I,"z4.");
		put J;
	end;
run;

proc iml;
	file log;
	do I=1 to 10;
		J=putn(I,"z4.");
		put J;
	end;
	closefile log;
quit;

%macro REPEAT;
	%do I=1 %to 10;
		%let J=%sysfunc(putn(&I.,"z4."));
		%put &J.;
	%end;
%mend;
%REPEAT;

I just realized that, unlike the rest cases, I should drop the double quotation marks around z4. in the PUTN, but I can't understand why the quotation marks, which are necessary for the rest cases, must be suppressed here. Why do they have different grammatical rules?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

No quotes in the z4 in the last one.

 

In macro language all quotes are dropped in literals. I think it’s because of how the macro processor works and it parses it all as text unless specified otherwise. 

 

https://stats.idre.ucla.edu/wp-content/uploads/2016/02/bt185.pdf

 

More generally, it’s a rule In macro language, docs:

Because %SYSFUNC is a macro function, you do not need to enclose character values in quotation marks as you do in DATA step functions. For example, the arguments to the OPEN function are enclosed in quotation marks when the function is used alone, but do not require quotation marks when used within %SYSFUNC()...
 

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1o13d7wb2zfcnn19s5ssl2zdxvi.htm&docse...

 

 


@Junyong wrote:

The code below must do the same thing in three different ways using the DATA step, SAS/IML, and SAS Macro, respectively, but unfortunately the third one doesn't work and just produces some warning messages saying "WARNING: Argument 2 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range."

data _null_;
	do I=1 to 10;
		J=putn(I,"z4.");
		put J;
	end;
run;

proc iml;
	file log;
	do I=1 to 10;
		J=putn(I,"z4.");
		put J;
	end;
	closefile log;
quit;

%macro REPEAT;
	%do I=1 %to 10;
		%let J=%sysfunc(putn(&I.,"z4."));
		%put &J.;
	%end;
%mend;
%REPEAT;

I just realized that, unlike the rest cases, I should drop the double quotation marks around z4. in the PUTN, but I can't understand why the quotation marks, which are necessary for the rest cases, must be suppressed here. Why do they have different grammatical rules?


 

View solution in original post

2 REPLIES 2
Reeza
Super User

No quotes in the z4 in the last one.

 

In macro language all quotes are dropped in literals. I think it’s because of how the macro processor works and it parses it all as text unless specified otherwise. 

 

https://stats.idre.ucla.edu/wp-content/uploads/2016/02/bt185.pdf

 

More generally, it’s a rule In macro language, docs:

Because %SYSFUNC is a macro function, you do not need to enclose character values in quotation marks as you do in DATA step functions. For example, the arguments to the OPEN function are enclosed in quotation marks when the function is used alone, but do not require quotation marks when used within %SYSFUNC()...
 

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1o13d7wb2zfcnn19s5ssl2zdxvi.htm&docse...

 

 


@Junyong wrote:

The code below must do the same thing in three different ways using the DATA step, SAS/IML, and SAS Macro, respectively, but unfortunately the third one doesn't work and just produces some warning messages saying "WARNING: Argument 2 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range."

data _null_;
	do I=1 to 10;
		J=putn(I,"z4.");
		put J;
	end;
run;

proc iml;
	file log;
	do I=1 to 10;
		J=putn(I,"z4.");
		put J;
	end;
	closefile log;
quit;

%macro REPEAT;
	%do I=1 %to 10;
		%let J=%sysfunc(putn(&I.,"z4."));
		%put &J.;
	%end;
%mend;
%REPEAT;

I just realized that, unlike the rest cases, I should drop the double quotation marks around z4. in the PUTN, but I can't understand why the quotation marks, which are necessary for the rest cases, must be suppressed here. Why do they have different grammatical rules?


 

novinosrin
Tourmaline | Level 20

the answer to your question is best understood by reviewing how tokenisation of literal tokens and special tokens work at compile time and what tokens are sent to compiler for datastep execution

Doesn't take too long to grasp as that's mostly common to many programming languages. hth

 

 

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
  • 2 replies
  • 6946 views
  • 0 likes
  • 3 in conversation