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:
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!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).Update: I solved the problem using arrays, as suggested in the solutions.
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.