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?
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 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.
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.);
Maxim 1: Read the Documentation
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.
If both variables are strings, why do you need the putn function?
Try to drop it:
WHERE CLOSINGCYCLE = %substr(&_CYCLE.,1,6);
@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)" ;
And you can avoid any macro coding altogether:
where closingcycle = " " !! substr("&_CYCLE.",1,6);
Which makes it clearer that you do want two leading blanks.
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 25. Read more here about why you should contribute and what is in it for you!
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.