DATA Step, Macro, Functions and more

assign and resolve macro keyword

Reply
Occasional Contributor
Posts: 5

assign and resolve macro keyword

Hi All,

I have below macro code. When i try to execute i am having error.

%macro test;

      %let lb_adt = %str(2011-01-03);

      %let var =  %NRBQUOTE('     ' On %sysfunc(strip(&lb_ADT)) of PCS  %unquote(%do k = 1 %to 3;lb_val&k %endSmiley Wink);

      %put &var;

%mend test;

%test;

Here is log messages:

4095%macro test;
4096%let lb_adt = %str(2011-01-03);
4097%let var =  %NRBQUOTE(' ' On %sysfunc(strip(&lb_ADT)) of PCS  %unquote(%do k = 1 %to 3;lb_val&k %endSmiley Wink);

ERROR: Macro keyword DO appears as text.  A semicolon or other delimiter may be missing.

ERROR: There is no matching %DO statement for the %END. This statement will be ignored.

4098%put &var;

4099   %mend test;

NOTE: The macro TEST completed compilation with errors.

  8 instructions 200 bytes.

4100

4101   %test;

ERROR: Expected close parenthesis after macro function invocation not found.

WARNING: Apparent symbolic reference K not resolved.

NOTE: Line generated by the invoked macro "TEST".

1    lb_val&k));
     ------
     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

I have also tried using the  %unquote and %nrstr to resolve but still having error messages.

%macro test;

      %let lb_adt = %str(2011-01-03);

      %let var =  %NRBQUOTE('     ' On %sysfunc(strip(&lb_ADT)) of PCS  %unquote(%nrstr(%do k = 1 %to 3;lb_val&k %endSmiley Wink));

      %put &var;

%mend test;

%test;

Here is log for above code:

ERROR: The %DO statement is not valid in open code.

WARNING: Apparent symbolic reference K not resolved.

ERROR: The %END statement is not valid in open code.

'     ' On 2011-01-03 of PCSlb_val&k

I am not sure what kind of mistake i am doing here.  Can someone provide any help?

Srisoham

Super User
Posts: 5,071

Re: assign and resolve macro keyword

You're making this problem much more complex than it needs to be.  How about starting this way:

%macro test;

   %local k;

   %global var;

   %do k=1 to 3;

         %let var = &var lb_val&k;

   %end;

   %let var = %str(   On ... &var);

%mend test;

Occasional Contributor
Posts: 5

Re: assign and resolve macro keyword

Thank you very much.  This helped me temporarily resolution.  Is there way send the macro instruction resolve it later?

The reason is in the above code, at DO loop the reference range high values are dynamic based the pre information availability.

I want to write it something like

%let txt = %nrbquote( %if &val ^=   %then %do;not missing %end;%else %do missing%endSmiley Wink;

Sri

Super User
Posts: 5,071

Re: assign and resolve macro keyword

The easiest way uses CALL SYMPUT instead of macro quoting.  That makes it easy to suspend resolution until later.  For example:

data _null_;

call symput('var', 'Some long string that includes &val2'Smiley Wink;

run;

Either earlier or later:

%if %length(&val) > 0 %then %let val2=non missing;

%else %let val2 = missing;

Then later on when you need it:

%let txt = &var;

There are other ways, including macro quoting.  This is just the way I would choose.

Good luck.

Ask a Question
Discussion stats
  • 3 replies
  • 846 views
  • 6 likes
  • 2 in conversation