BookmarkSubscribeRSS Feed
Tzur
Calcite | Level 5

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!

 

3 REPLIES 3
Reeza
Super User

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. 

Tzur
Calcite | Level 5

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?

Reeza
Super User

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1352 views
  • 1 like
  • 2 in conversation