Help using Base SAS procedures

Same variable name in 2 tables

Reply
N/A
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


Thanks

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: 174

Re: Same variable name in 2 tables

Posted in reply to deleted_user
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)
[pre]
data table3(drop=Table1_Var Table2_Var);
merge table1(rename=(var=Table1_Var))
table2(rename=(var=Table2_Var));
by common_var;
NewVar = sum(Table1_Var,Table2_Var);
run;
[/pre]
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
N/A
Posts: 0

Re: Same variable name in 2 tables

Posted in reply to GertNissen
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,426

Re: Same variable name in 2 tables

Posted in reply to deleted_user
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).

/Linus
Data never sleeps
N/A
Posts: 0

Re: Same variable name in 2 tables

Posted in reply to deleted_user
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 ?

PeterC
Ask a Question
Discussion stats
  • 4 replies
  • 115 views
  • 0 likes
  • 3 in conversation