BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

I would like to understand what is causing the error in the code fragment below. It appears to me that this is simply two different ways to form a macro value, but somehow, something is different.

An explanation or pointers to the proper manual pages for clarification would be appreciated.

TIA

Ken

Consider the following code:

/* Define format via macro variables */

%let whole_len = 8;

/* Method #1 */

%let testfmt1 = comma&whole_len..2;

/* Method #2 */

%let testfmt2 = %str(comma)&whole_len%str(.2);

/* Echo to the log */

%put testfmt1 = &testfmt1;
%put testfmt2 = &testfmt2;

/* Ask if the variables are the same */

%macro testfmt;
%if &testfmt1 = &testfmt2 %then
%put Formats Match!;
%else
%put Formats do not Match!;
%mend testfmt;

%testfmt;

/* Apply the first format - it works! */

data testfmt1;
a = 123.45;
b = put( a, &testfmt1 );

put a=;
put b=;
run;

/* Apply the second format - it fails! (!@#$%!$%!!!) */

data testfmt2;
a = 123.45;
b = put( a, &testfmt2 );

put a=;
put b=;
run;

Here is the log:

NOTE: Copyright (c) 2002-2003 by SAS Institute Inc., Cary, NC, USA.
NOTE: SAS (r) 9.1 (TS1M3)
Licensed to ...
NOTE: This session is executing on the XP_PRO platform.
NOTE: SAS 9.1.3 Service Pack 4

NOTE: SAS initialization used:
real time 0.42 seconds
cpu time 0.29 seconds


NOTE: AUTOEXEC processing beginning; file is C:\Tools\SAS\autoexec.sas.

NOTE: Libref LIBRARY was successfully assigned as follows:
Engine: V9
Physical Name: E:\SASWork\Library

NOTE: AUTOEXEC processing completed.

1
2 %let whole_len = 8;
3
4 %let testfmt1 = comma&whole_len..2;
5 %let testfmt2 = %str(comma)&whole_len%str(.2);
6
7 %put testfmt1 = &testfmt1;
testfmt1 = comma8.2
8 %put testfmt2 = &testfmt2;
testfmt2 = comma8.2
9
10 %macro testfmt;
11 %if &testfmt1 = &testfmt2 %then
12 %put Formats Match!;
13 %else
14 %put Formats do not Match!;
15 %mend testfmt;
16
17 %testfmt;
Formats Match!
18 run;
19
20 data testfmt1;
21 a = 123.45;
22 b = put( a, &testfmt1 );
23
24 put a=;
25 put b=;
26 run;

a=123.45
b=123.45
NOTE: The data set WORK.TESTFMT1 has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


27
28 data testfmt2;
29 a = 123.45;
200: LINE and COLUMN cannot be determined.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL may allow recovery of the LINE and COLUMN where the error has
occurred.
ERROR 200-322: The symbol is not recognized and will be ignored.
30 b = put( a, &testfmt2 );

------
85
76
ERROR 85-322: Expecting a format name.

ERROR 76-322: Syntax error, statement will be ignored.

31
32 put a=;
33 put b=;
34 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TESTFMT2 may be incomplete. When this step was stopped there were 0 observations and 2
variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
4 REPLIES 4
Cynthia_sas
SAS Super FREQ
Hi,
This isn't really an ODS related question. However, if you try putting this before your code:
options symbolgen mprint mlogic;
when you run your code, you will see the following message:

SYMBOLGEN: Macro variable TESTFMT2 resolves to comma8.2
SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing.


Although the use of %str to form &TESTFMT2 seems like it's harmless, it can cause unprintable hex characters to be placed in the value, as described here:
http://support.sas.com/faq/005/FAQ00531.html

Since &testfmt1 works (without using %STR), then either use that method or the %UNQUOTE method described in the note.

For more help with macro quoting functions, you could read the SAS Macro documentation or contact Tech Support. There are also a number of good books available through SAS Press that cover the SAS Macro facility.
cynthia
deleted_user
Not applicable
Thanks for the help.

Regrets for posting in this forum - I did not see one for1 BASE or macros. Can you please direct me?

Thanks again,

Ken
Cynthia_sas
SAS Super FREQ
Ken:
I don't think there is a forum for just Base SAS programming questions. However, your best resource on that subject are the Tech Support FAQs or calling Tech Support for help. Calls from people with questions are how Tech Support keeps building the FAQ.
Here's how to get to the searchable FAQ:
http://support.sas.com/techsup/faq/products.html
Your products of interest would be BASE SAS and MACRO facility and DATA STEP Programming.
cynthia
Olivier
Pyrite | Level 9
In the second method, you should add a %UNQUOTE when using the value of the macro-variable outside any macro statement (not necessary when you use %IF or %PUT, which automatically dequote macro-quoted strings).

data testfmt2;
a = 123.45;
b = put( a, %UNQUOTE(&testfmt2) );
put a=;
put b=;
run;

It does work.

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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