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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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