Search code examples
sassas-macro

Macro variables inside data step


I need to save the value of a certain variable in a data step in a macro and then use that macro in the same data step. i tried with SYMPUT, but if I've understood correctly a macro variable created that way cannot be used inside the same data step (it's assigned at the end of the data step, I guess?)

Here is a simplified example. I have a list of data fields t1,...,t100 representing something happening at certain times, events being represented by numbers, and a data field t_start giving me the starting time for the process in which I'm interested for every observation. I want to check if I have all the data, and drop the observation otherwise. I want to proceed as follows.

DATA WANT;
    SET HAVE;

    CALL SYMPUT('START_TIME', t_start);

    DO I=&START_TIME. TO 100;
        IF t_&I. = . THEN DELETE;
    END;
RUN;

This doesn't work, I think for the reasons mentioned above. Is there a workaround?

Remarks:

  1. I simplified the situation, the real case I'm looking at is more complex (e.g. the variables are not called t1,...t100 but something with a bit more structure). If possible, I would like to get something as close to the approach I gave above as possible, as different solutions might not be applicable to my case. Of course, if this is not possible then any solution is more than welcome!
  2. I tried looking at RESOLVE, but it doesn't seem to do what I'm looking for (or at least I don't understand it well enough to make it do what i desire).
  3. As a last resort, I could try to solve the problem using two data steps, one defining the macro variables, the other one using it to check and delete the undesired observations. I would prefer avoiding this solution if possible.

Update: I solved the problem using arrays, as suggested in the solutions.


Solution

  • You are trying to use the value of the dataset variables t_start and i to figure out which variable to test. That is what arrays are for.

    DATA WANT;
      SET HAVE;
      array t t1-t100;
      DO I=t_start TO 100;
        IF t(i) = . THEN DELETE;
      END;
    RUN;
    

    No need for macro variables, much less macro variables that can travel backwards in time and modify the code of a data step after it has already started running.