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:
- 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! - 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). - 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.
t_&i
will not work because you don't have an I macro variable. I suspect you really need arrays but without more details that's all I can say now. – Reezat_start
directly unfortunately won't work in my "true" case, because int_start
i have a string, and only a part of the string is the particle of the other variables (theI
). Maybe there's a way to do it like that, I'll try on Monday if i don't find any direct solution. – Daniel Robert-NicoudSTART_TIME
variable, e.g. by%LET START_TIME_BIS = %SUBSTR(START_TIME,1,2);
or something like that. But the line of reasoning would remain the same. – Daniel Robert-Nicoud