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!
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.