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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.