BookmarkSubscribeRSS Feed
StickyRoll
Fluorite | Level 6

I have a question about the error message that i encountered.

 

I have a macro variable named _CYCLE that has the string value 20180831 (8 characters).

In the macro test(), there is a dataset where its variable "CLOSINGCYCLE" is a character type and is currently storing value 201808 (just 6 characters).

 

Hence, in the where clause, i first substr it to only taking the first 6 characters and then putn (convert it to character). 

 

%let _CYCLE = 20180831;

%macro test();
data SAP;
set OrigSAP;
WHERE CLOSINGCYCLE = putn(%substr(&_CYCLE.,1,6), 8.);         
%END;
run;

%mend;
%test;

However, the error message that i am getting is:

SYMBOLGEN:  Macro variable _CYCLE resolves to 20180831
MPRINT(test):   WHERE CLOSINGCYCLE = putn(201808, 8.);
ERROR: Function PUTN requires a character expression as argument 2.

What could be wrong here?

7 REPLIES 7
mkeintz
PROC Star

You don't need to use PUTN.

 

Since your variable CLOSINGCYCLE is character, (length 6), you, can change the WHERE statement to just use the   =:   (equals then colon) comparison operator against the constant generated by the expression "&_CYCLE".   The =: comparison takes the shorter character value (in _closingcycle) and compares it to an equal length substring of the longer character value (i.e. the first six characters of &_CYCLE).

 

 

where closingcycle =:  "&_CYCLE";

which produces the code

where closingcycle =: "20180831";

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
s_lassen
Meteorite | Level 14

The error message you are getting is simply that the second argument should be a character string with the format you want to use. Like this:

 72         data _null_;
 73           a=putn(%substr(&_CYCLE.,1,6), '8.');
 74           put a $quote.;
 75         run;
 
 "  201808"

This produces a right-aligned string of length 8. If that is what you want, I would suggest using

WHERE CLOSINGCYCLE = "  %substr(&_CYCLE.,1,6)";

which is a much simpler way to get the right-aligned string.

 

 

Tom
Super User Tom
Super User

The error message tells you want is wrong:

ERROR: Function PUTN requires a character expression as argument 2.

So either make the second argument a character expression:

putn(%substr(&_CYCLE.,1,6), '8.');   

Or just use the PUT() function instead since that want an actual FORMAT as the second argument, not a character expression.

put(%substr(&_CYCLE.,1,6), 8.);   
Kurt_Bremser
Super User

Maxim 1: Read the Documentation

PUTN Function 

You will see that the second argument to the function has to be a character expression, not a format name which has to be interpreted by the DATA step compiler (as happens with the PUT and INPUT functions). So you either have to use the PUT function, or enclose the format specification in quotes. The former is recommended when a fixed format is used.

PUTC, PUTN, INPUTC, INPUTN must be used within %SYSFUNC, as the data step compiler is not available there.

Shmuel
Garnet | Level 18

If both variables are strings, why do you need the putn function?

Try to drop it:

WHERE CLOSINGCYCLE = %substr(&_CYCLE.,1,6); 
Tom
Super User Tom
Super User

@StickyRoll wrote:

I have a question about the error message that i encountered.

 

I have a macro variable named _CYCLE that has the string value 20180831 (8 characters).

In the macro test(), there is a dataset where its variable "CLOSINGCYCLE" is a character type and is currently storing value 201808 (just 6 characters).

 

Hence, in the where clause, i first substr it to only taking the first 6 characters and then putn (convert it to character). 

Why would you need to convert a string back to a string??? 

 

To specify a string constant in SAS code just enclose the text in quotes. Use double quotes so that the macro processor will "see" the %SUBSTR() function call.

WHERE CLOSINGCYCLE = "%substr(&_CYCLE.,1,6)" ;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 1565 views
  • 4 likes
  • 6 in conversation