BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cougar300
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
cougar300
Calcite | Level 5

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.

Shmuel
Garnet | Level 18

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;
cougar300
Calcite | Level 5

Perfect!  Thanks!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 690 views
  • 0 likes
  • 3 in conversation