Hi, I'm looking for a more efficient way to do this than what I've come up with. I need to multiply QUANTITY by COST, but I want COST to exist in a separate table so that that calculations will carry through when they are updated.
Table_1:
ACCOUNT QUANTITY
Bob 5
Louis 10
Sarah 12
Table_2:
COST
5,200
Table_I_Want:
ACCOUNT QUANTITY COST TOTAL
Bob 5 5,200 26,000
Louis 10 5,200 52,000
Sarah 12 5,200 62,400
Right now I'm creating a lookup field for each table and then merging the tables together, but I'm assuming there is a more efficient way.
Thanks!
This is what I am currently doing.
Table_1:
ACCOUNT QUANTITY LookUpKey
Bob 5 Service_Key
Louis 10 Service_Key
Sarah 12 Service_Key
Table_2:
COST LookUpKey
5,200 Service_Key
SAS Code:
Data Table_I_Generate;
merge Table_1 Table_2;
by LookUpKey;
run;
Table_I_Generate:
ACCOUNT QUANTITY LookUpKey COST
Bob 5 Service_Key 5,200
Louis 10 Service_Key 5,200
Sarah 12 Service_Key 5,200
Code:
data Table_With_Total
set Table_I_Generate(drop=LookUpKey);
Total = Quantity * Cost;
Drop
Well, if you want to keep the quantity in a separate table from the cost, then you will have to merge the two tables together somehow.
So, is there a more efficient way? I don't think so, unless you are doing extremely inefficient merging now. Can you show us how you are doing this today?
This is what I am currently doing.
Table_1:
ACCOUNT QUANTITY LookUpKey
Bob 5 Service_Key
Louis 10 Service_Key
Sarah 12 Service_Key
Table_2:
COST LookUpKey
5,200 Service_Key
SAS Code:
Data Table_I_Generate;
merge Table_1 Table_2;
by LookUpKey;
run;
Table_I_Generate:
ACCOUNT QUANTITY LookUpKey COST
Bob 5 Service_Key 5,200
Louis 10 Service_Key 5,200
Sarah 12 Service_Key 5,200
Code:
data Table_With_Total
set Table_I_Generate(drop=LookUpKey);
Total = Quantity * Cost;
Drop
That's what is required. I doubt it could be made simpler.
Now if you have a much larger data sets, it probably will take more time to do the merge ... what specifically is the problem you are trying to eliminate or reduce?
"...what specifically is the problem you are trying to eliminate or reduce?"
Nothing in particular. As I'm learning to code I'm trying to eliminate as many workarounds and be as efficient as possible while avoiding bad habits.
Thanks a lot for the feedback, it was very helpful!
You present the problem as one in which the cost "table" has a single value. After all you are using it as a constant for all three records in the quantity table. Is that true, or can you have differing table_2 costs for differing accounts in table_1?
Yes, it would be a constant value for every record. It would not change from one record to the next.
When I do this in Excel, I just reference the cell where it exists in the calculation. For example, if it is in cell A1 I would multiply each row by $A$1.
I've got about 13 years experience doing pretty involved calculations in Excel but have only been programming in SAS for ~6 months, so I'm still getting used to manipulating databases. It looks like there's some subtle but important differences.
Thanks!
If it's a constant pull that into a macro variable and use it in your calculations. You can also keep it in a view so it is up to date every time you 'look' at the table. If you're not familiar with views, it may be beneficial in this case.
*create macro variable with cost;
proc sql noprint;
select cost into :cost_value from table_2;
quit;
data want / view=want;
set have;
Total = &cost_value * Quantity;
run;
@BarryP wrote:
Yes, it would be a constant value for every record. It would not change from one record to the next.
When I do this in Excel, I just reference the cell where it exists in the calculation. For example, if it is in cell A1 I would multiply each row by $A$1.
I've got about 13 years experience doing pretty involved calculations in Excel but have only been programming in SAS for ~6 months, so I'm still getting used to manipulating databases. It looks like there's some subtle but important differences.
Thanks!
@BarryP wrote:
Yes, it would be a constant value for every record. It would not change from one record to the next.
When I do this in Excel, I just reference the cell where it exists in the calculation. For example, if it is in cell A1 I would multiply each row by $A$1.
SAS can't do things the way Excel does things. You'll just have to learn to think differently.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.