MACRO ERROR - Apparent symbolic reference not resolved

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

MACRO ERROR - Apparent symbolic reference not resolved

All I need to do is make the last datastep generate the dataset hpi2 after reading dataset hpi1, but I get am "Apparrent symbolic reference not resolved" error. Why is this the case and how can I remedy it? I attach my original textfile which creates the original hpi0.

data hpi0;
   infile '/fmac/users/f371032/HPI.txt' dlm='090D'x LRecL=10000;
   input location $2. month1-month15;
   format month1-month15 f16.12;
run;

options mprint mlogic symbolgen;

%let No=0;
%let hpi&No=hpi0;
%let setNo=%eval(&No+1);

************************************THIS IS WHERE MY PROBLEMS BEGIN**************************************************************
data hpi&setNo; ----------------------------->This dataset is generated correctly; hpi1 dataset is created
  set &&hpi&No;
  %test();
  %let arrlowbound=%eval(&arrlowbound+3);
  %let No=%eval(&No+1);
  %let setNo=%eval(&setNo+1);
run;

%put hpi&setNo &&hpi&No; -------------> This statement tells me that I have the appropriate values for hpi&setNo and &&hpi&No, ie set

data hpi&setNo; -------------> This is the dataset that shows up with no observations, when I would expect it to read dataset hpi1 and generate hpi2

  set &&hpi&No;

run;

Attachment

Accepted Solutions
Solution
‎06-13-2014 04:23 PM
Super User
Super User
Posts: 7,039

Re: MACRO ERROR - Apparent symbolic reference not resolved

Posted in reply to maroulator

Try to simplify things.

I would eliminate the %TEST() macro and just put the logic inside of your existing macro.

Looks like you are running this inside of a loop but it is not clear what you are looping over. 

Below is a example of logic but it obviously needs something to make the looping stop.

%* Setup Initial Values ;

%let Current=0;

%let DropLowerBound=1;

%let LastVar = 6; %* Not sure where this will come from ;

%do %while (1) ;

  %let Next = %eval(&Current + 1);

  %let DropUpperBound = %eval(&DropLowerBound + 3);

data hpi&Next;

  set hpi&Current;

  drop Var&DropLowerBound - Var&DropUpperBound ;

  %do i= %eval(&LastVar + 1) %to %eval(&LastVar + 3) ;

    var&i = var&lastvar;

  %end;

run;

%* Increment variables for next time through the loop ;

  %let Current = %eval(&current + 1);

  %let DropLowerBound = %eval(&DropLowerBound +3 );

  %let LastVar = %eval(&LastVar + 3);

%end;

View solution in original post


All Replies
PROC Star
Posts: 1,322

Re: MACRO ERROR - Apparent symbolic reference not resolved

Posted in reply to maroulator

Hi,

More questions:

Could you post the log from running your code, with MPRINT option on (and maybe symbolgen too)?

What is %test() doing?  Perhaps show its definition.

Where do you get the apparent symbolic reference not resolved error?

&arrlowbound is not defined anywhere in the code shown.  If you create it in %test(), you would need to make sure to declare it to be %Global.

It's odd to have %LET statements "inside" a data step.  Macro statements such as %LET execute independently of (and before) SAS code (DATA step or PROC step).   Better form to put them after the RUN statement, to show that they are not part of the DATA step.

--Q.

Frequent Contributor
Posts: 122

Re: MACRO ERROR - Apparent symbolic reference not resolved

Below please find my full code, which ought to clarify your questions. I need to take in the HPI dataset, which contains a large number of similar variables (hence the array definition), drop the first three (array) variables, add three new variables, and output a new dataset which will be used as an input into the next iteration of the "add three-drop three process." It's easy enough, however, I am trying to come up with more efficient code, which is what has me stumped. Again, my problem lies with the last datastep, whereby (at this stage) I am trying to replicate the newly created dataset.

Thanks in advance for any insight.

/*Read HPI dataset into SAS*/

data hpi0;
   infile '/fmac/users/f371032/HPI.txt' dlm='090D'x LRecL=10000;
   input location $2. month1-month15;
   format month1-month15 f16.12;
run;

options mprint mlogic symbolgen;

%let No=0;
%let hpi&No=hpi0;
%let setNo=%eval(&No+1);

%let arrlowbound=1;

/*Creating macro to drop three (array) variables, then add three new variables, and output the new dataset*/

       %macro test();
          %let arrelements=18;
          %let arrupbound=%eval(&arrlowbound+17);
          array month{&arrelements} month&arrlowbound-month&arrupbound;
          format month&arrlowbound-month&arrupbound f16.12;
          %let dropzone=%eval(&arrlowbound+2);
          drop month&arrlowbound-month&dropzone;
               do i=dim(month)-2 to dim(month);
                  month{i}=month{dim(month)-3};
               end;
               drop i;
               output;
       %mend test;

/*Applying test macro to newly created dataset*/


data hpi&setNo;
  set &&hpi&No;
  %test();
  %let arrlowbound=%eval(&arrlowbound+3);
  %let No=%eval(&No+1);
  %let setNo=%eval(&setNo+1);
run;

%put &&hpi&setNo &hpi&No;

/*Trying to replicate the dataset created in the datastep above*/

data hpi&setNo;
  set &&hpi&No;
run;

PROC Star
Posts: 1,322

Re: MACRO ERROR - Apparent symbolic reference not resolved

Posted in reply to maroulator

Hi,

Sorry, I'm still not seeing the logic.  Can you show the log where you get the unresolved message?

Just looking at the code, without understanding the logic, would suggest you try to simplify it to something like: (untested)

%let No=0;
%let arrlowbound=1;

data hpi%eval(&no+1);
  set hpi&No;
  array month{*} month&arrlowbound-month%eval(&arrlowbound+17);
  format month&arrlowbound-month%eval(&arrlowbound+17) f16.12;
  drop month&arrlowbound-month%eval(&arrlowbound+2) i;
  do i=dim(month)-2 to dim(month);
    month{i}=month{dim(month)-3};
  end;
run;


%let arrlowbound=%eval(&arrlowbound+3);
%let No=%eval(&No+1);
 
%put hpi%eval(&no+1) &hpi&No;


/*Trying to replicate the dataset created in the datastep above*/
 
data hpi%eval(&no+1);
  set hpi&No;
run;

If you are planning to iterate this "add 3 drop 3" process, you could make a macro that would do that.

But I'm hesitant to suggest more.  The logic is very odd.  Note for example your do loop will set all 3 new month variables (month16-month18) to the value of month15.

Maybe if you describe the general problem (do you really need all these datasets with different subsets of variables?), someone will suggest an alternative appraoch.  What is the final output dataset you need to produce?

Super User
Super User
Posts: 7,942

Re: MACRO ERROR - Apparent symbolic reference not resolved

I concur, not really grasping the purpose of this code.  Why not just normalize (set the data down the table rather than across) then you could just loop over the data per a datastep i.e.

normalize dataset:

location     month     Result

US            1             0.984900000000

US            2             0.984900000000

...

data my_first_process;

     set normalized_dataset end=last;

     where 3 <= month <= 15;

     if last then do;

          output;

          do i=16 to 19;

               month=i;

               value=0; /* update per your logic */

               output;

          end;

     end;

     else output;

run;

If you wanted to go further the above should be straight forward to macro-tize, or execute in a call execute to repeat many times.

Frequent Contributor
Posts: 122

Re: MACRO ERROR - Apparent symbolic reference not resolved


Attached is the log from running the code below. The test macro is my "drop 3-add 3 macro" and I went with it b/c it was what popped in my head at the time and it worked within the time constraints that I had. Also, the three variables that I add to generate the new dataset need to equal the value of the last variable in the original dataset.

Attached is also the code I ended up using and that I am trying to make more efficient; the "drop 3 - add 3"macro works by creating an array of 18 elements that gets updated everytime the macro is called. I need to generate a total of 9 new datasets; my problem is that I am using too many data steps, which I would like to suppress into one macro that takes the output of the previous datastep and uses it as input into the next dataset that gets generated. I've come far enough and I think that I'm onlly missing something trivial.

Thanks for all the help so far, any further suggestions would be appreciated.

Attachment
Attachment
Super User
Posts: 19,772

Re: MACRO ERROR - Apparent symbolic reference not resolved

Posted in reply to maroulator

1. In a data step you use call symput to create a macro variable not %let.

2. The macro variable won't be available in the datastep, try calculating it before. You'll also need to modify the ones in %test

%let arrlowbound=%eval(&arrlowbound+3);     

data hpi4; 

    set hpi3;

    %test();

run;

Super User
Posts: 19,772

Re: MACRO ERROR - Apparent symbolic reference not resolved

Posted in reply to maroulator

This still doesn't make sense to me as why you'd want to do this, but here's a solution that should work:

data example;

  infile 'C:\_LOCALdata\example.txt' dlm='090D'x ;

  informat location $2.;

  input location  month1-month18;

  format month1-month18 f16.12;

run;

options symbolgen mprint;

%macro add3(datain=example, add=3, name=hpi, iterations=4);

*find max month and min month in the dataset;

proc sql;

    select max(input(compress(name, ,'kd'), 3.)),  

            min(input(compress(name, , 'kd'), 3.)),

            max(input(compress(name, ,'kd'), 3.)) - min(input(compress(name, , 'kd'), 3.)) + 1

                    into :max_month, :min_month, :array_length

    from sashelp.vcolumn

    where libname="WORK"

    and upcase(memname)=upcase("&datain")

    and upcase(name) like "MONTH%";

quit;

%put &max_month;

%put &min_month;

%put array_length;

*calculate names of vars to be dropped;

*calculate names of vars to be reassigned;

%do n=1 %to &iterations;

    %let max_month_end=%eval(&max_month+&add);

    %let max_month_start=%eval(&max_month+1);

    %let min_month_start=%eval(&min_month);

    %let min_month_end=%eval(&min_month+&add-1);

    %put MAX MONTH END: &max_month_end;

    %put MAX MONTH START: &max_month_start;

    %put MIN MONTH END: &min_month_end;

    %put MIN MONTH START: &min_month_start;

    data hpi&n.;

        %if &n=1 %then %do;

            set &datain (drop= month&min_month_start. - month&min_month_end.);

        %end;

        %else %do;

            set &&name%eval(&n-1) (drop= month&min_month_start. - month&min_month_end.);

        %end;

        array month(%eval(&min_month_end+1):&max_month_end) month%eval(&min_month_end+1) - month%eval(&max_month_end);

        do i=&max_month_start to &max_month_end;

            month(i)=month(&max_month_start-1);

        end;

    run;

    %let min_month=%eval(&min_month + &add);

    %let max_month=%eval(&max_month + &add);

    drop i;

%end;

%mend;

%add3(datain=example, add=3, name=hpi, iterations=4);

Solution
‎06-13-2014 04:23 PM
Super User
Super User
Posts: 7,039

Re: MACRO ERROR - Apparent symbolic reference not resolved

Posted in reply to maroulator

Try to simplify things.

I would eliminate the %TEST() macro and just put the logic inside of your existing macro.

Looks like you are running this inside of a loop but it is not clear what you are looping over. 

Below is a example of logic but it obviously needs something to make the looping stop.

%* Setup Initial Values ;

%let Current=0;

%let DropLowerBound=1;

%let LastVar = 6; %* Not sure where this will come from ;

%do %while (1) ;

  %let Next = %eval(&Current + 1);

  %let DropUpperBound = %eval(&DropLowerBound + 3);

data hpi&Next;

  set hpi&Current;

  drop Var&DropLowerBound - Var&DropUpperBound ;

  %do i= %eval(&LastVar + 1) %to %eval(&LastVar + 3) ;

    var&i = var&lastvar;

  %end;

run;

%* Increment variables for next time through the loop ;

  %let Current = %eval(&current + 1);

  %let DropLowerBound = %eval(&DropLowerBound +3 );

  %let LastVar = %eval(&LastVar + 3);

%end;

Frequent Contributor
Posts: 122

Re: MACRO ERROR - Apparent symbolic reference not resolved

Posted in reply to maroulator

Ladies and Gentlemen,

This problem has generated a lot of traffic, so I'd like to thank everyone who stepped up and offered insight into solving this problem. All of you contributed to its solution in small or large way by steering me in helpful directions and helping avoid others; I also appreciated the remarks on simplifying my logic. I now have a code that works.

A note on the awarding of the points, especially to those who didn't get any points out of me despite their helpful insight (which I cannot tell you how much I appreciate). It seems that the system only allows me to award only one correct and two helpful answers, so I literally ran out of awards. Those of you who did not receive any "remuneration" from me on this one, please accept my sincerest apologies. I know who you are and I will be sure to pay you on the next occasion going forward. Again, please accept my apologies.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 1004 views
  • 9 likes
  • 5 in conversation