Solved
Contributor
Posts: 33

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: 33

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

All Replies
Super User
Posts: 13,941

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: 33

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.

Posts: 5,625

Re: sum case expression returning boolean values

CASE WHEN N1 < N2 THEN MS1 ELSE MS2 END

PG
Contributor
Posts: 33

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: 13,941

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: 33

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.