DATA Step, Macro, Functions and more

macro variable

Reply
Frequent Contributor
Posts: 115

macro variable

Macro variable contains a character &. It looks like Cu Australia, New Zealand & Fiji.

Var1 = Cu Australia, New Zealand & Fiji

I am passing this variable

%macro test(name1='&Var1')

I am getting results as 'Cu Australia, New Zealand & Fiji', but i dont want to see the single quotes.

without  quote i am getting error as ERROR: All positional parameters must precede keyword parameters.

PROC Star
Posts: 7,492

Re: macro variable

Posted in reply to sunilreddy

It won't resolve unless you use double rather than single quotes.

Frequent Contributor
Posts: 115

Re: macro variable

Posted in reply to sunilreddy

%mrp_sdc(orgid=&&CUID&x, orgnm="&&CUNM&x");

I've used double quotes while passing macro variable  as above, but i am getting below error in macro function

title " #3A&slideNo.#&levelName - &&orgnm. ";

SYMBOLGEN:  Macro variable ORGNM resolves to "Cu Australia, New Zealand & Fiji                                                     

                          "

NOTE: Line generated by the macro variable "ORGNM".

2039        " #3A001#VDP Radio Units - "Cu Australia, New Zealand & Fiji

            ____________________________

            49

2039     !         "

WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.


PROC Star
Posts: 7,492

Re: macro variable

Posted in reply to sunilreddy

It will be easier to look at if you post your full code.

SAS Super FREQ
Posts: 8,868

Re: macro variable

Hi:

  I agree with Art. Seeing snippets of code doesn't help here. But meanwhile, here are some references that may help you understand why the , and & are causing issues.

http://support.sas.com/resources/papers/proceedings10/029-2010.pdf

 

http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#n1oerrlvth1qy5n1b...

  

http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#p0pwrvnlcooi3tn0z...

cynthia

Super Contributor
Posts: 644

Re: macro variable

Posted in reply to sunilreddy

Another approach to try is to wrap the title in single quotes, but mask them during macro resolution

See if this works

title %unquote(%str(%') #3A&slideNo.#&levelName - &&orgnm. %str(%')) ;

Pace Cynthia there is at least one situation where you want quotes in a macro variable:  where item IN (&list)

with &list being a set of string variables

Richard in Oz

SAS Super FREQ
Posts: 8,868

Re: macro variable

Posted in reply to RichardinOz

Richard:

  Yes, you're right! So I'll amend my earlier declaration -- you almost never need to "pre-quote" macro variables unless you're using strings in IN lists or another type quoting situation required by the syntax.

  But, a pre-quoted IN list could also cause issues in a TITLE statement, too -- if you pre-quoted with double quotes. Once you understand what's happening and how to deal with it, it is something you CAN deal with...by using single quotes for the IN list and then using double quotes for the TITLE.

%let inlist = %str('Alfred', 'Barbara', 'James');

   

proc print data=sashelp.class;

  where name in (&inlist);

  title "Want Students: &inlist";

run;

title;


I didn't see the need for unquote or dequote at this point, given what we've been told about the problem. So, I tested something like this...going with the simplest quoting function first.

%macro putit(myvar=);

%put myvar= &myvar;

  

proc print data=sashelp.class(obs=2);
  title "&myvar";
run;
title;

 
%mend putit;

  

options mprint symbolgen;

 
ods html file='c:\temp\usemacro.html';
%putit(myvar=%nrstr(1- Fred, Ethel, Lucy & Ricky))

%putit(myvar=%nrstr(2-Cu Australia, New Zealand & Fiji))

 

%putit(myvar=%nrstr(3- Kermit & Elmo Get 50% Each))

 
ods html close;

cynthia

N/A
Posts: 1

Re: macro variable

Posted in reply to sunilreddy

How about try : %macro test(name1=%str(&Var1))





SAS Super FREQ
Posts: 8,868

Re: macro variable

Hi,

  If the macro variable value being passed as a parameter, does NOT contain any macro triggers (% or &), then perhaps %STR would work. If the macro variable DOES contain macro triggers, then %NRSTR would be the place to start. However, there might be compile time vs execution time issues, as well, which is why I posted the links to the doc that talk about when to use which macro quoting functions.

  Also, it is never a good idea to use quotes in a macro variable value because, as seen with the error message in the TITLE statement, the quotes that surround the macro variable will terminate the quotes that start the TITLE statement and cause the rest of the title text to be placed in open code and generate the "ambiguous" title warning.

   So, for this TITLE statement (TITLE statement quotes are in RED)

title " #3A&slideNo.#&levelName - &&orgnm. ";


  When resolution happens, the SYMBOLGEN clearly shows that quotes are part of the macro variable value being passed (and the quotes would be unnecessary if the correct macro quoting function was used) -- quotes added to macro variable value are shown in BLUE

SYMBOLGEN:  Macro variable ORGNM resolves to

"Cu Australia, New Zealand & Fiji "

NOTE: Line generated by the macro variable "ORGNM".

2039       " #3A001#VDP Radio Units - "Cu Australia, New Zealand & Fiji

            ____________________________

            49

2039     !        "

WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.

So, the first BLUE quote terminate the first RED quote in the resolved title statement, leaving the Cu Australia, New Zealand & Fiji text hanging out as unquoted text. If the correct macro quoting function was used, no error would be generated.

 

cynthia

Super User
Super User
Posts: 7,079

Re: macro variable

Posted in reply to sunilreddy

You have to do something to the value of a macro parameter to protect commas, ampersands or other special macro characters.

You could add code where the macro variable is referenced inside the macro to remove the quotes.  For example you might try using the DEQUOTE() function.

%macro test(var1);

  %put Dequoted value= %qsysfunc(dequote(&var1));

%mend test;


%test(var1='High & Low');

Dequoted value= High & Low


%test(var1=Normal);

Dequoted value= Normal


%let X=High;

%test(var1='&x');

Dequoted value= &x


%test(var1="&x");

Dequoted value= High


Super User
Posts: 5,518

Re: macro variable

Posted in reply to sunilreddy

If you don't want single quotes, leave them out.

As you have seen, the comma causes a problem because it marks the end of a macro parameter's value.  There are ways around it (some shown above by other posters) possibly this variation as well:

%macro test (name1=%superq(var1));

It is not clear from your post why you would want to set up a default value for &name1.  But the syntax would be equally simple if you just used it to call the macro later without using a default value:

%test (name1=%superq(var1))

Good luck.

Ask a Question
Discussion stats
  • 10 replies
  • 779 views
  • 0 likes
  • 7 in conversation