DATA Step, Macro, Functions and more

sum case expression returning boolean values

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

sum case expression returning boolean values

Hi Community,


Well this my code 

 

DATA ABC;

INFILE DATALINES MISSOVER DSD;

INPUT N1 N2 MS1$ MS2$;

DATALINES;

10,12,MS,

10,12,MS

10,12, ,MS

10,12,MS,

;

RUN;

 

PROC SQL;

   CREATE TABLE WORK.BBC

   SELECT N1, 

          N2, 

          ms1, 

          ms2, 

          

            (CASE WHEN(N1<N2)THEN MS1 OR MS2 END) AS FINAL_MS

      FROM WORK.ABC;

QUIT;

 

IN BBC  i want to create a new variable i.e. FINAL_MS in order to receive values from either ms1 or ms2 but it returning me logical value that is 1 as n1<n2.  My need is it must receive either value from ms1 or ms2. How to do this ?  thanks


Accepted Solutions
Solution
‎11-29-2016 01:31 PM
Contributor
Posts: 29

Re: sum case expression returning boolean values

 

 

PROC SQL;

CREATE TABLE CBC AS

 

SELECT N1,N2,MS1,MS2,CASE WHEN(N1<N2) THEN MS1 OR MS2 END AS FINAL_MS FROM ABC;

QUIT;

 

------------------------------------------------- following code worked well---------------------------------------

 

PROC SQL;

CREATE TABLE CBC AS

 

SELECT N1,N2,MS1,MS2,CASE WHEN(N1<N2) THEN COALESCE(MS1,MS2) END AS FINAL_MS FROM ABC;

QUIT;

 

thanks ballardw

 

View solution in original post


All Replies
Super User
Posts: 10,484

Re: sum case expression returning boolean values

Please provide details on which value, MS1 or MS2, should be returned. Your current code or description does not provide anything to tell use which of those values should be returned.

Contributor
Posts: 29

Re: sum case expression returning boolean values

[ Edited ]

As i said i want to populate final_ms by getting values either MS1 variable or MS2 variable.

And values of MS1 & MS2 are already listed in datalines.  

Respected Advisor
Posts: 4,644

Re: sum case expression returning boolean values

CASE WHEN N1 < N2 THEN MS1 ELSE MS2 END

PG
Contributor
Posts: 29

Re: sum case expression returning boolean values

PG i have other conditions too to write further, cant simply put ms2 in else. 

simply all i want is final_ms should take value either from ms1 or ms2 whenever condition becomes true (n1<n2). thats it.

Super User
Posts: 10,484

Re: sum case expression returning boolean values

Lets try this again:

 

Your data currently shows that you may have missing for one of MS1 or MS2 variables. If that is always the case and you mean that you want the non-missing value as the result then perhaps you want

 

CASE WHEN(N1<N2)THEN coalescec(MS1,MS2) END

 

It really helps to show the exact desired result from your example data.

Solution
‎11-29-2016 01:31 PM
Contributor
Posts: 29

Re: sum case expression returning boolean values

 

 

PROC SQL;

CREATE TABLE CBC AS

 

SELECT N1,N2,MS1,MS2,CASE WHEN(N1<N2) THEN MS1 OR MS2 END AS FINAL_MS FROM ABC;

QUIT;

 

------------------------------------------------- following code worked well---------------------------------------

 

PROC SQL;

CREATE TABLE CBC AS

 

SELECT N1,N2,MS1,MS2,CASE WHEN(N1<N2) THEN COALESCE(MS1,MS2) END AS FINAL_MS FROM ABC;

QUIT;

 

thanks ballardw

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 342 views
  • 1 like
  • 3 in conversation