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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

6 REPLIES 6
ballardw
Super User

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;
Kurt_Bremser
Super User

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;
Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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.

debw16
Fluorite | Level 6

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.

sorosch
Fluorite | Level 6

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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 4508 views
  • 3 likes
  • 6 in conversation