how does sas calculates the value of a case statement - mysql

I don't understand how sas calculates the value for PATIENT , I've looked everywhere but there seems to be no documentation for how sas, calculates the formulas in the case parenthesis, I only know that when the value of
PATIENT is even the CASE expression has a Boolean result of 1 , 0 or 2, but I just don't see how to get the value out of the expression, could someone please explain me?
PROC SQL;
CREATE TABLE TESTMED AS
SELECT PATIENT,
CASE ((PATIENT/2 = INT(PATIENT/2)) +
(PATIENT = .))
WHEN 1 THEN 'Med A' WHEN 0 THEN
'Med B' ELSE 'Error' END AS DOSEGRP
LENGTH=5
FROM VITALS
ORDER BY PATIENT;
QUIT;

I can't see any error.
if patient could be . 0 1 2 this will be (first expression + second expression):
0 ---> {0=0}true(1) + {patient=.} false(0) = 1
1 ---> {0.5=0}false(0) + {patient=.} false(0) = 0
2 ---> {1=1}true(1) +{patient=.} false(0) = 1
. ---> {.=.} true(1) + {patient=.} true(1) = 2
If you want to see the result of that boolean expression you can simply use:
PROC SQL;
CREATE TABLE TESTMED AS
SELECT patient,
((PATIENT/2 = INT(PATIENT/2)) + (PATIENT = .)) AS result
LENGTH=5
FROM VITALS
ORDER BY PATIENT;
QUIT;
and you will see that the results are correct.
So your boolean expression could assume only 3 values:
(1=0+1)
(0=0+0)
(2=1+1)
When 0 you assign MEDA;
When 1 you assign MEDB;
When 2 you assign ERROR;
And ERROR is assigned for missing values.
1st expression:
{missing/2=missing} = {int(missing)=missing} [errors are reported as missing]
missing=missing ---> true ---> 1
2nd expression:
missing=missing ---> true ---> 1
result:
1+1=2 ---> ERROR

SAS evaluates boolean expressions to 1 (true) or 0 (false). So your expressions is the sum of two of these. So the possible answers are 0, 1 or 2.
The first expression, (PATIENT/2 = INT(PATIENT/2)), will be true when PATIENT is even or missing. The second expression, (PATIENT = .), will be true when PATIENT is missing. So a result of 2 means that PATIENT is missing a result of 1 means that PATIENT is even and a result of 0 means that patient is odd.
So you could rewrite the case statement as:
CASE WHEN MISSING(PATIENT) then 'Error'
WHEN MOD(PATIENT,2)=1 then 'Med A'
ELSE 'Med B'
END AS DOSEGRP

Related

MySQL query to get count in a range

A snapshot of my DB
I want to get count of pcl_No present in a specified range( SUM(tv_land + tv_imp))
I have written a query but I am getting Invalid use of group error
select
count(CASE WHEN sum(tv_land+tv_imp) BETWEEN 0 AND 10000 THEN 1 END) as count0_10000
from
tax_l2006
where red_date = 0
GROUP BY pcl_no
Sample Output
0-10000 10000-20000 30000-40000 >40000
2 3 1 8
I think you just want:
select count(CASE WHEN tv_land + tv_imp BETWEEN 0 AND 10000 THEN 1 END) as count0_10000
from tax_l2006
where red_date = 0;
I would write this in MySQL more simply as:
select sum( (tv_land + tv_imp) between 0 and 10000 ) as count0_10000
from tax_l2006
where red_date = 0;
MySQL treats a true boolean as "1" in a numeric context and "0" for false, so summing the boolean is counting the number of times it is true.
You can follow the same pattern for the rest of the columns.
If pcl_no is not unique and you need to sum the values, then you would use a subquery:
select sum(total between 0 and 10000) as count0_10000
from (select pcl_no, sum(tv_land + tv_imp) as total
from tax_l2006
where red_date = 0
group by pcl_no
) p;
However, your data suggests that the column is unique, so the additional aggregation is not necessary.

how to sum after iff condition in ssrs?

I want to sum all the values of the column, using if condition same as the column expression:-
The Expression of the normal column:-
iif(Fields!mcount.Value <> 0 And Fields!TaxCode.Value = 1 ,Fields!InputAmnt.Value,0)
The Expression to sum, I used 2 codes and the same result "#Error" appeared in both cases
sum(iif(Fields!mcount.Value <> 0 And Fields!TaxCode.Value = 1 ,Fields!InputAmnt.Value,0))
and
iif(Fields!mcount.Value <> 0 And Fields!TaxCode.Value = 1 ,sum(Fields!InputAmnt.Value),0)
any help ?
Try adding VAL from the InputAmnt field. See below expression.
=SUM(iif(Fields!mcount.Value <> 0 And Fields!TaxCode.Value = 1 ,VAL(Fields!InputAmnt.Value),0))

MYSQL- Multiple column value change

I'm trying to change the values of 150 columns to the following;
'0 = Not provided'
' 1 = Yes '
' 2 = No '
I was able to do this using a case statement for each column. But the problem is it creates puts everything into one column. Is there a way to do it for each individual column without writing 150 case statements? The columns need to be in a specific order.
example:
SELECT CASE
WHEN Answer.Question1_ID is null THEN 'Not Provided'
WHEN Answer.Question1_ID = 1 THEN 'Yes'
WHEN Answer.Question1_ID = 2 Then 'No'
End as 'Question1',
CASE
WHEN Answer.Question2_ID is null THEN 'Not Provided'
WHEN Answer.Question2_ID = 1 THEN 'Yes'
WHEN Answer.Question2_ID = 2 Then 'No'
End as 'Question2'
.
.
.
From Answer
Is there a way to do it for each individual column without writing 150 case statements?
No.
You can use a program to write the case statements if need be.

How to select a row by a certain amount of similar data

I'm about to build some sort of function or query where I can check if a certain record already exists in the database. The following rules apply:
The table has 6 columns
My yet-to-build-query has access to a complete row-object (all 6 values)
This query should find each row with at least 4 out of 6 corresponding values from the object I passed
Using MySQL
Is it even possible to build a query like this? My goal is to have a function which can return true if it's likely that a row like the passed object is already existing in the database.
Is my only option to make a query with multiple where-statements (where I try for each combination 4 different values)?
pseudo:
function getSimilarRow(Row_Object $row)
{
//select *
//from table_x
//where 4 out of 6 properties from object $row apply
}
You could use a case statement in the where clause for each property you are trying to match. If it meets the criteria then give the case statement a value of 1; if it doesn't then give it 0. The sum of the cases should then be >= 4.
I'm not that familiar with MySQL but the following will work (I knocked up a quick SQL Fiddle to show it working):
select * from SomeTable where
(case when propertyOne = 'value1' then 1 else 0 end) +
(case when propertyTwo = 'value2' then 1 else 0 end) +
(case when propertyThree = 'value3' then 1 else 0 end) +
(case when propertyFour = 'value4' then 1 else 0 end) +
(case when propertyFive = 'value5' then 1 else 0 end) +
(case when propertySix = 'value6' then 1 else 0 end) >= 4
Obviously you could change your logic in each clause if you'd prefer them to be likes or anything. You could even apply a weighting to each column by using something other than just 1 if you needed to get really creative.

Can I Add SQL QUERY in EXPRESSION MAPFILE?

Can I Add SQL QUERY in EXPRESSION MAPFILE ? like this ..
CLASS
Name '> 0'
EXPRESSION (SELECT * from tb_kelurahan where id_kecamatan='[id_kecamatan]' > 0)
COLOR 20 215 0
OUTLINECOLOR 0 0 0
END # Class
No. But as an alternative you can add the sql as an additional column to the DATA statement and then reference that in the expression.
DATA "the_geom from (select case when id_kecamatan>0 then 1 else 0 end as myexpress, the_geom, ...) as foo ...."
and then use:
EXPRESSION ([myexpress] = 1)