06-30-2017 12:38 AM
I have a program that inlcudes a loop that reads different data sets and then do different operations depening on the data set.
for example, say i have data A (with variables ID, A1 and A2) and data B (with variables ID, B1 and B2). and the ID variable has either A or B, depending on which data set it is.
I have a loop in which i read the data set and then does (this is a simplification of the code, just to illustrate):
if ID='A' and A1=A2 then C=3;
if ID='B' and B1=B2 then C=2;
The problem is that when data A is read, SAS creats variables B1 and B2, and when data B is read, it creates varaible A1 and A2. How can I avoid that?
06-30-2017 01:28 AM
I am actually trying this now and it seems to work, but I would still like to understnad why SAS behave in this way. Basically, why does it creates variables that are not in the data set?
In this case, it creates varaibles B1 and B2 eventhough the first part of the if condition is not met (ID does not equal B). I thought that in that case, SAS would just ignore the rest of the statement, but instead, it creates these variables. I'd like to know why it does that and if there is a way around it?
06-30-2017 01:46 AM
One of the drawbacks of a language that doesn't require you to declare variables.
It assumes the variable exists and that you'll use it accordingly.
As as far as I know there isn't a way to change the default behaviour. You could try and get around it with by using VVALUEX instead
If vvaluex(id||'1')=vvaluex(id||'2') then do; If id='A' then C=2; If id='B' then C=3; End;