Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

update column based on values in another (SAS, SQL)

I have a table with 2 columns, I need to update one of them based on values in another.

Column A is a flag (1s and 0s), column B is a string column. I need update column B based on the flag in column A. If A = 1 then column B needs to preserve its values, if column A is 0, I need to set column B to blank.

column A.    Column B.      
1            abc         <-keep
0            abc         <-set to blank
1            abc         <-keep


PROC SQL;
update table abc
set column B = 
case when A.column IS NOT NULL then A.column = A.column
else A.column = ' '
end  

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Simply using Update with CASE statement to do what you want:

update column B based on the flag in column A. If A = 1 then column B
needs to preserve its values, if column A is 0, I need to set column B
to blank.

UPDATE
  your_table
SET
  column_B = CASE
            WHEN column_A = 1 THEN column_B -- columnB needs to preserve its value
            ELSE ''  -- set column B to blank.
  END;

Before:

column_A    column_B
1           abc
0           abc
1           abc

After update:

column_A    column_B
1           abc
0           
1           abc

dbfiddle DEMO

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading