Dataset is simple, Brand, Brand2, and value, so it looks something like this:
Brand Brand2 Amount
A B -5
A C -7
B A -10
etc
I want to add two variables to the dataset, one is simply the absolute value of AMOUNT for that record.
The tricky one is that I need another variable, call it REVERSE, that would be the AMOUNT of the opposite brands, so for the first record, Brand =A and Brand2=B, I want REVERSE to grab the value of the record where Brand=B and Brand2=A (reverse value of the brands chosen).
So the first record would be Brand=A Brand2=B AMOUNT=-5 ABSAMT=5 and REVERSE=-10
I have it like this, but I don't know how to get the REVERSE to work
proc sql;
create table brand12 as select a.brand,a.brand2, abs(a.amount) as absamt, sum(amount) having (a.brand=b.brand2 and a.brand2=b.brand) as reverse
from main_data a ,main_data b;
quit;
I've tried the reverse a couple different ways, but none have worked. That is the last one I tried, but I think this is simple.
Any help on syntax would be apprecitated. Thanks!
Try next code:
proc sql;
create table want as select
a.*,
abs(a.amount) as absamt,
b.amount as reverse
from have as a
left join have as b
on a.barnd=b.brand2 and
a.brand2=b.brand;
quit;
It's difficult if not impossible to work with just 3 records as an example, as it is unlikely that this covers all possible examples. Furthermore you don't tell us what value of the REVERSE variable should be assigned to record 2.
Can we have a larger data set to work with, that covers realistic examples of these variables?
I was just showing some example data. if that was a full dataset, there would be no reverse for record 2, but the full datset would be exhausive where every combination of brand and brand2 are in the dataset.
I just don't know the syntax to call the value from the opposite order of brand and brand2.
Try next code:
proc sql;
create table want as select
a.*,
abs(a.amount) as absamt,
b.amount as reverse
from have as a
left join have as b
on a.barnd=b.brand2 and
a.brand2=b.brand;
quit;
Perfect! Thanks!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.