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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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