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-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!

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.

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