BookmarkSubscribeRSS Feed
SASGeek
Obsidian | Level 7

Hello all,

 

I have a prefix macro variable that changes depending on the location and use in the program. What I need is to append the prefix and make it into a quoted value. For example:

 

%let prefix = U;

 

Data AAA;

 if type = &prefix.101AB then action = 'movement';

run;

 

Therefore type has to be 'U101AB' (with the single quotes).

 

How do I do that?

 

Thanks

 

Paula

6 REPLIES 6
ballardw
Super User

No it doesn't "need to be in single quotes".

Macro variables will not resolve inside single quotes but  to compare a value double quotes are just fine.

 

Data AAA;
  /* you need some sort of SOURCE for the variable Type though*/
  if type = "&prefix.101AB" then action = 'movement';
run;
SASGeek
Obsidian | Level 7

Thank you.

I tried that and it only resolves to U101AB not 'U101AB'. 

%let prefix = U;

Data AAA;
type = "&prefix.101AB";
run;

 

 

SASGeek_0-1703190860356.png

 

Tom
Super User Tom
Super User

@SASGeek wrote:

Thank you.

I tried that and it only resolves to U101AB not 'U101AB'. 

%let prefix = U;

Data AAA;
type = "&prefix.101AB";
run;

 

 

SASGeek_0-1703190860356.png

 


If you want the extra quotes in the VALUE of the variable you have type those also.

So include single quotes in the value just use.

type = "'&prefix.101AB'";

But to include double quotes you will need to double them up so the parser knows where the string constant ends.

type = """&prefix.101AB""";
PaigeMiller
Diamond | Level 26

@SASGeek wrote:

Thank you.

I tried that and it only resolves to U101AB not 'U101AB'. 

%let prefix = U;

Data AAA;
type = "&prefix.101AB";
run;

 

 

SASGeek_0-1703190860356.png

 


In SAS, when a character variable is displayed, the display does not show quotes. In most usages, you do not need to see the quotes around the value U101AB. Of course, it sure would be helpful if you showed us how this variable named TYPE is going to be used.

--
Paige Miller
ballardw
Super User

@SASGeek wrote:

Thank you.

I tried that and it only resolves to U101AB not 'U101AB'. 

%let prefix = U;

Data AAA;
type = "&prefix.101AB";
run;

 

 

SASGeek_0-1703190860356.png

 


It is very poor practice to keep changing anything related to example code. Your first example does a comparison of a variable that has no value assigned. You do not show any actual example on an existing variable with quoted values.

It is actually  not difficult to create values with quotes but it can be difficult to actually use them for logic.

So show use actual values of your existing variable such as with proc print.

 

%let prefix = U;
Data AAA;
type = quote("&prefix.101AB","'");
run;

Is one way to force a single quote into a variable.

Now, show use how you actually expect to use it.

 

 

 

 

Cynthia_sas
SAS Super FREQ

Hi,

  Normally, character variables in SAS do NOT internally retain quotes. The place where you usually need to use quotes is in IF statements, and WHERE statements or any place where you are using a quoted string to set the value of a character variable.

  So, going back to your original post, where you showed an IF statement, I made some fake data and tested your IF statement, as shown below:

Cynthia_sas_0-1703203383036.png

  As you can see, the IF statement worked correctly for purposes of the comparison when the IF statement used "&prefix.101AB" in the comparison with the TYPE variable value. And, as you can see in the LOG, the internally stored value for TYPE does NOT contain quotes, just as the internally stored value of ACTION does not contain quotes.

  It's really not clear to me what you need to do or why you need to store quotes for the value of TYPE. The way SAS usually works would be to NOT store quotes for character variables.

Cynthia

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 1003 views
  • 1 like
  • 5 in conversation