Hello,
I have a table such as
ID Sales Year Sales_classification
1 12 2008 4567
1 11 2008 7890
1 13 2008 5678
1 14 2009 4567
1 15 2009 7890
1 16 2009 5678
2 8 2008 4567
2 1 2008 7890
2 3 2008 5678
2 4 2009 4567
2 5 2009 7890
2 6 2009 5678
Condition includes:
IF
With the same ID
With the same YEAR
Sales_classification first digit larger than 6
THEN
sum the SALES meet all the conditions above
This may be done with or without CONDITION proc, there may be several ways and logic to do this.
Would you please help me with it?
Thank you very much!
Then switch to:
proc sql;
create table want as
select
ID,
year,
sum(case when first(cats(Sales_classification)) > "6"
then Sales
else 0 end) as sumSales
from have
group by id, year;
quit;
You could do:
proc sql;
create table want as
select ID, year, sum(Sales) as sumSales
from have
where first(cats(Sales_classification)) > "6"
group by id, year;
quit;
OMG
You are awesome! It exactly does what I want. It is so neat and clean.
But the outcome table only shows the observation meet the condition. What if I also want those obs show "0" if do not meet the condition?
Then switch to:
proc sql;
create table want as
select
ID,
year,
sum(case when first(cats(Sales_classification)) > "6"
then Sales
else 0 end) as sumSales
from have
group by id, year;
quit;
Great!
Thank you!
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.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.