Question

Running into something a bit unusual. I feel like this is probably a silly error on my part, but for the life of me I can't figure out what it could be.

The following works fine:

declare hash hash&foo1.&foo2.&foo3.&foo4.();
...

The following does not work:

declare hash hash%do bar = 1 %to &nvars.;&&foo&bar..%end;();
...

They both appear to resolve as

declare hash hash1111();

But the latter throws a syntax error. Any thoughts? Specific error is:

Line generated by the macro variable "FOO1".
1
Error 22-322: Syntax error, expecting one of the following: a name, (, ;.
...
declare hash hash1111();
NOTE: Line generated by the macro variable "FOO4".
hash1111
Was it helpful?

Solution

If I want to generate code fragments, I do this before I use the fragment.
The following code is for illustration:

%macro test;
%global foobar;
%let foobar = ;
%let foo1 = 1;
%let foo2 = 1;
%let foo3 = 1;
%let foo4 = 1;

%let foobar = &foo1.;
%do bar = 2 %to 4;
  %let foobar = %sysfunc(cats(&foobar., &&foo&bar..));
  %put *&foobar.*;
%end;
%mend;
%test;

Now you kann use the macrovariable foobar wherever you like. e.g.

declare hash hash&foobar.();  

Aftersome research I finally found a solution in http://www2.sas.com/proceedings/sugi28/011-28.pdf

You need to unquote the macro loop. I don't know why, but the following code is working:

%macro test3();  
  %global foobar;  
  %let foobar =;  
  %let foo1 = 1;  
  %let foo2 = 1;
  %let foo3 = 1;
  %let foo4 = 1;

  data test2;
    declare hash hash%unquote(%do bar = 1 %to 4;&&foo&bar..%end;)();
  run;
%mend;  
%test3;

The following statement of the SUGI-paper lead to solution: "In any case, the older documentation gave the simple rule:
If the mprint looks good and the SAS compiler does not understand it, then try %UNQUOTE."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top