BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

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?

6 REPLIES 6
David_Billa
Rhodochrosite | Level 12
Before I provide the sample data, can I understand that the provided
program matches with the conditions in ' case when ' to create new
variable?
Satish_Parida
Lapis Lazuli | Level 10

@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;
Satish_Parida
Lapis Lazuli | Level 10

@David_Billa If the issue is resolved please close the issue. 
If not please provide us some test data.

David_Billa
Rhodochrosite | Level 12

@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

 

 

ballardw
Super User

@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.

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3999 views
  • 1 like
  • 4 in conversation