BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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 🐵
4 REPLIES 4
GertNissen
Barite | Level 11
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
deleted_user
Not applicable
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?
LinusH
Tourmaline | Level 20
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
deleted_user
Not applicable
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 1268 views
  • 0 likes
  • 3 in conversation