BookmarkSubscribeRSS Feed
tomcmacdonald
Quartz | Level 8

Let's say I have two tables that are being merged and I need to access the value of a column in one to determine the value of a new column in the resulting join.  For instance:

 

data foo;
  input foo $;
  datalines;
a
b
c
d
;
run;

data bar;
  input foo $;
  datalines;
a
b
c
d
;
run;

data merged;
  merge foo(in=a) bar(in=b);
  by foo;
  if a.foo = 'a' then myvar = 1;
run;

I'm getting this error:

 

ERROR: DATA STEP Component Object failure.  Aborted during the COMPILATION phase.
ERROR 557-185: Variable a is not an object.

How can I check the value of table foo column a within the DATA step? 

8 REPLIES 8
tomrvincent
Rhodochrosite | Level 12
if foo = 'a' then myvar = 1; works
Reeza
Super User

When variables are merged with the same name, the one on the right overwrites the first one, so you need to rename them so they're different variables and then do a comparison. 

 

Or you may be able to get what you want here with:

 

if a and foo='a' then ....
SuryaKiran
Meteorite | Level 14

IN= is a dataset option that will give Boolean values (Always 0 or 1). It is not an alias name for the dataset like in sql alias reference. 

 

Creates a Boolean variable that indicates whether the data set contributed data to the current observation.

 

http://support.sas.com/documentation/cdl/en/ledsoptsref/69751/HTML/default/viewer.htm#n1p1o2dsuc465n...

Thanks,
Suryakiran
tomcmacdonald
Quartz | Level 8

What if the table and the variable have the same name?  Let's say there's a table a and a column a in that table.  Can you do something like this:

 

if a and a = 0 then myvar = 1;
Reeza
Super User

That would be silly programming to set the IN= variable to one that already exists....not that people don't do that I suppose. I would consider that user error. I get wanting to understand the theory though.


@tomcmacdonald wrote:

What if the table and the variable have the same name?  Let's say there's a table a and a column a in that table.  Can you do something like this:

 

if a and a = 0 then myvar = 1;

 

tomcmacdonald
Quartz | Level 8

I'm just thinking from a SQL perspective where you can do something like this:

 

case
  when a.a = 0 then 1
end as myvar
Reeza
Super User

In a data step you don't reference the table name though. You can find a record using the IN but the individual access to the different variables with the same names isn't easily possible. You can probably access it somehow, but that's beyond my skills. Most like @Tomor @data_null__ , or @Ksharp may know the answer. 

 

 

Tom
Super User Tom
Super User

@tomcmacdonald wrote:

I'm just thinking from a SQL perspective where you can do something like this:

 

case
  when a.a = 0 then 1
end as myvar

Doesn't really make much sense.

There is just one set of variables in a data step. No matter how many inputs you are reading or output you are writing.

If you need to reference both variables then one will need to have its name changed.

 merge a(rename=(a=a_from_a)) b;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 826 views
  • 0 likes
  • 5 in conversation