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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 769 views
  • 0 likes
  • 3 in conversation