Help using Base SAS procedures

Same variable name in 2 tables

Posts: 0

Same variable name in 2 tables

I have 2 programs that have pretty much the same sets of code in each one, but each one uses to different tables/datasets to perform the same calculations.

There is a point in each set of programs where they need to pull a resulting variable from a table/dataset from the other program, but this/these variables have the same variable name.

Is there a way of referencing the variables buy table/dataset name and variable name, e.g.

Table1 = Table/dataset
Table2 = Table/dataset
Var = Variable appearing in both Table1 & Table2

NewVar = Table1.Var + Table2.Var


P.s I am a newbie to SAS code at the moment so as simple as possible would be great Smiley Surprised)
SAS Employee
Posts: 160

Re: Same variable name in 2 tables

If you are using a SAS data step, you can't reference a varibale by dataset, like you would in i.e. SQL. (I guess you are doing a merge)
data table3(drop=Table1_Var Table2_Var);
merge table1(rename=(var=Table1_Var))
by common_var;
NewVar = sum(Table1_Var,Table2_Var);
The rename will not change table1 and table2, the rename will only work in the current datastep.

Using "+" instead of "SUM()" can/will give wrong results if you have missing values - I changed that in my example.

Message was edited by: Geniz
Posts: 0

Re: Same variable name in 2 tables

Is it possible to do this merge as well as another merge in the same program, i.e perform 2 merges within the same code?
Super User
Posts: 5,256

Re: Same variable name in 2 tables

Yes, you can merge as many tables as you like in the same data step, as long as you merge on a common BY variable (unlike SQL, where you can join tables on different columns).

Data never sleeps
Posts: 0

Re: Same variable name in 2 tables

you might be able to make two different merges in one data step, (assumed to be independent merges), but I don't see why you would want to do that. Can you explain the purpose ?

Ask a Question
Discussion stats
  • 4 replies
  • 3 in conversation