Hello,
I have the following code:
%macro test; %let word='abcde'; data _null_; _notValidSign = substr(&word., 2, 1); put 'ERROR: ' _notValidSign; run; %mend test; %test;
I just want to set quotation marks around the variable _notValidSign.
So the output looks like:
ERROR: "b"
I tried e.g.:
put 'ERROR: ' ""_notValidSign"";
And i get the output with a blank - why is this?:
ERROR: "b ";
Can someone help please?
Thank you very much
Best regards
George
First, check your posts to make sure they are legible. Sometimes, depending on how you paste things the box makes the code hard to read.
If you want to quote text, use the correct function: QUOTE.
Use of the multiple quote marks is likely to introduce issues as you have found.
Note that the quote function may result with blanks as well depending on what the argument is. If the value returned has a length longer the actual value it will contain trailing blanks. If you want to ensure there are never trailing blanks then use the STRIP function to remove them: quote( strip ( <some character value>))
The below works as desired because you are explicitly returning exactly one character from the result of the substr function.
%macro test; %let word='abcde'; data _null_; _notValidSign = quote(substr(&word., 2, 1)); put 'ERROR: ' _notValidSign; run; %mend test; %test;
First, check your posts to make sure they are legible. Sometimes, depending on how you paste things the box makes the code hard to read.
If you want to quote text, use the correct function: QUOTE.
Use of the multiple quote marks is likely to introduce issues as you have found.
Note that the quote function may result with blanks as well depending on what the argument is. If the value returned has a length longer the actual value it will contain trailing blanks. If you want to ensure there are never trailing blanks then use the STRIP function to remove them: quote( strip ( <some character value>))
The below works as desired because you are explicitly returning exactly one character from the result of the substr function.
%macro test; %let word='abcde'; data _null_; _notValidSign = quote(substr(&word., 2, 1)); put 'ERROR: ' _notValidSign; run; %mend test; %test;
As usual, when macro language is involved, people tend to out-think themselves:
%let word=abcde;
data _null_;
length _notValidSign $1;
_notValidSign = substr("&word.", 2, 1);
out = 'ERROR: "' !! _notValidSign !! '"';
put out;
run;
The major factor you are overlooking as that the PUT statement automatically leaves a blank after writing a variable. You can control that:
put _notValidSIgn $1.;
A secondary issue that doesn't come into play here: the length of _notValidSIgn is $5. When SUBSTR creates a new variable, it automatically assigns that variable the same length as the incoming string.
Do you want the quotes in the value or just in the output?
If just in the output then use the $QUOTE. format. You might want to add the : modifier to avoid including the trialing spaces inside the quotes.
When defining your variable "word" , do not use quotes. Quotes aren't needed for a macro variable. It would look like:
%let word = abcde;
See if that helps solve the isse.
Thank you very much to you all for your help. Sometimes you are really on the hose....
So easy...
Thanks to all
Best reagrds
George
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.