I would like to understand whether code below logically is right. Because it is not producing the desired results. I want to create a variable 'CR_Ratio' which is equal to (CR/sum(Yearly_Premium) when it statisfies the condition
Contract='yes' and Start_CR='no' and year_start='yes' otherwise I want '0'
proc sql;
create table want as
select * ,(case when (Contract='yes' and Start_CR='no' and Full_year_start='yes')
then (CR/sum(Yearly_Premium
else 0))
end) as CR_Ratio length = 8
format = 21.4
from have
group by
Segment,
Company
;
quit;
what are other ways to tackle it apart from case when?
Please supply an example for have, point out where the result does not meet your expectations, and show what you expected.
@David_Billa You misplaced few parenthesis. Here is the code you need.
proc sql;
create table want as
select * ,(case when (Contract='yes' and Start_CR='no' and Full_year_start='yes')
then CR/sum(Yearly_Premium)
else 0)
end) as CR_Ratio length = 8 format = 21.4
from have
group by
Segment,
Company
;
quit;
@David_Billa If the issue is resolved please close the issue.
If not please provide us some test data.
@Satish_Parida @Kurt_Bremser I don't think I've misplaced any paranthesis in my post. Code works fine. I tried with your proposed code also but still the data is not matching to my desired results.
Sample data:
data have;
input Branch $ Sub_Segment $ Currency $ Yearly_Premium Contract $ Start_CR $ End_CR $ Full_year_Start RC;
datalines;
1831234 7183_AS1 EUR 2533629 yes no no yes 131169336
1831234 7183_AS1 EUR 11643516 yes no no yes 131169336
;
run;
Desired result is, I want all the rows and observations from the dataset 'have' plus one more variable 'CR_ratio' and it should have value as 9.2521 in the matching rows. I want to display 0 if the condition is not statisfied.
Condition to claculate new variable is,
(case
when (Contract='yes' and Start_CR='no' and Full_year_start='yes') then CR/sum(Yearly_Premium)
else 0)
I'm looking for the solution in proc sql
@David_Billa wrote:
@Satish_Parida @Kurt_Bremser I don't think I've misplaced any paranthesis in my post.
I'm looking for the solution in proc sql
Considering that the original post "code"
proc sql;
create table want as
select * ,(case when (Contract='yes' and Start_CR='no' and Full_year_start='yes')
then (CR/sum(Yearly_Premium
else 0))
end) as CR_Ratio length = 8
format = 21.4
from have
group by
Segment,
Company
;
quit;
Has the closing ) for both the "(CR/ " and "sum(Yearly_premium" after "else 0" very clearly, you did post code with bad parentheses. Which is one reason you will see a lot of requests to post code from the LOG in this forum. Then there is no question as to what was actually submitted.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.