DATA Step, Macro, Functions and more

if creates variables that are missing from the data

Reply
New Contributor
Posts: 2

if creates variables that are missing from the data

Hello,

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?

Thanks!

 

Super User
Posts: 17,888

Re: if creates variables that are missing from the data

Since you're likely in a macro already why not use macro variables here? Your pattern has a logic so it would make sense here. 

New Contributor
Posts: 2

Re: if creates variables that are missing from the data

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?

Super User
Posts: 17,888

Re: if creates variables that are missing from the data

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;
Ask a Question
Discussion stats
  • 3 replies
  • 113 views
  • 1 like
  • 2 in conversation