I would like to sum a column, "Sales", with the following conditions:
Can I do this with a Computed Column in Enterprise Guide? If not, please help with code. Thank you.
Thank you all for the great help.
I ended up using something similar to FredrikE, but with an "OR". I also added another WHEN statement I needed.
CASE WHEN ((Return_Amt + Store_Credit) = Sale_Amt) THEN Return_Amt WHEN Sale_Amt ne Return_Amt AND Sale_Amt ne Rebate_Amt THEN Sale_Amt ELSE 0 END
Then I sum the column in the next query.
For some reason when I put the first "WHEN" statement with the formula below the other, the Sale_Amt would populate, not the Return_Amt. I suppose the first "THEN" takes precedence on the CASE statement. Thank you, everyone.
@Fistful_Dollars wrote:
Can I do this with a Computed Column in Enterprise Guide? If not, please help with code. Thank you.
Yes you can use computed columns for this.
Create two columns using CASE statements, one for condition #1 and one for condition #2.
Then sum each of those new columns.
just expanding on what @Reeza suggested
sum( case when sales_amount ne Returns then sales_amount else 0 end) as computed_column1,
sum( case when sales_amount ne Rebates then sales_amount else 0 end) as computed_column2
@kiranv_One of those should be eq not ne 🙂
Why use two columns?
Why not just add this condition in the computed colums editor and then select aggregation sum?
case
when (Sales_amounts ne Returns OR Sales_amounts ne Rebates) then sales_amount)
else 0
end
Depending on the logic use AND instead of OR...
//Fredrik
@FredrikE is correct, I was reading that as creating two different sums. If it's a single sum you can definitely use the OR condition within the CASE statement.
Thank you all for the great help.
I ended up using something similar to FredrikE, but with an "OR". I also added another WHEN statement I needed.
CASE WHEN ((Return_Amt + Store_Credit) = Sale_Amt) THEN Return_Amt WHEN Sale_Amt ne Return_Amt AND Sale_Amt ne Rebate_Amt THEN Sale_Amt ELSE 0 END
Then I sum the column in the next query.
For some reason when I put the first "WHEN" statement with the formula below the other, the Sale_Amt would populate, not the Return_Amt. I suppose the first "THEN" takes precedence on the CASE statement. Thank you, everyone.
As soon as a case/when evaluation is true it stops the evaluation, so more important "when"'s should come first 🙂
//Fredrik
@Reeza you are right, looks like I did not understand the question properly
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.