<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Setting a column value with an existing value in case expressions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891677#M352240</link>
    <description>&lt;P&gt;All the variables are of 'character' data type. That's why I am confused as to why I am getting this error message.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe I have wrongly written the code as well?&lt;/P&gt;</description>
    <pubDate>Wed, 30 Aug 2023 11:51:49 GMT</pubDate>
    <dc:creator>MILKYLOVE</dc:creator>
    <dc:date>2023-08-30T11:51:49Z</dc:date>
    <item>
      <title>Setting a column value with an existing value in case expressions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891668#M352235</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table named 'role' with several columns. I want to change the value of one of its columns based on this scenario:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If CD_ROLE is empty, then put SOURCE in CD_ROLE and put NU_ROLE in NU_CLI&lt;/P&gt;&lt;P&gt;if CD_ROLE is not empty then do nothing&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Formats:&lt;/P&gt;&lt;P&gt;CD_ROLE : $2.&lt;/P&gt;&lt;P&gt;NU_ROLE: $20.&lt;/P&gt;&lt;P&gt;NU_CLI: $20.&lt;/P&gt;&lt;P&gt;SOURCE: $4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I tried:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;
CREATE TABLE r2;
SELECT r.*,
CASE WHEN CD_ROLE = ' ' THEN CD_ROLE = r.SOURCE 
WHEN CD_ROLE = ' ' THEN NU_CLI = r.NU_ROLE
ELSE CD_ROLE 
END AS CD_ROLE length = 20
FROM role r;
quit;&lt;/PRE&gt;&lt;P&gt;I am getting error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result of WHEN clause 3 is not the same data type as the preceding results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 10:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891668#M352235</guid>
      <dc:creator>MILKYLOVE</dc:creator>
      <dc:date>2023-08-30T10:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Setting a column value with an existing value in case expressions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891673#M352238</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/404650"&gt;@MILKYLOVE&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am getting error:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result of WHEN clause 3 is not the same data type as the preceding results.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This happens because somewhere, you are trying to assign a numeric variable to a character variable, or the other way around. So, what are the variable types (numeric or character) of the variables in use in this problem?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;From now on, when you get an error, show us the ENTIRE log for this PROC or DATA step. Do not show us the error message detached from the rest of the log for this PROC or DATA step.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 11:38:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891673#M352238</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-30T11:38:00Z</dc:date>
    </item>
    <item>
      <title>Re: Setting a column value with an existing value in case expressions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891677#M352240</link>
      <description>&lt;P&gt;All the variables are of 'character' data type. That's why I am confused as to why I am getting this error message.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe I have wrongly written the code as well?&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 11:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891677#M352240</guid>
      <dc:creator>MILKYLOVE</dc:creator>
      <dc:date>2023-08-30T11:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Setting a column value with an existing value in case expressions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891682#M352243</link>
      <description>&lt;P&gt;Please show us the ENTIRE log for this PROC SQL. Copy the log as text, and paste it into the window that appears when you click on the &amp;lt;/&amp;gt; icon.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PaigeMiller_0-1663012019648.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75161i0E71B1489A6C9839/image-size/large?v=v2&amp;amp;px=999" role="button" title="PaigeMiller_0-1663012019648.png" alt="PaigeMiller_0-1663012019648.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 11:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891682#M352243</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-30T11:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: Setting a column value with an existing value in case expressions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891743#M352251</link>
      <description>&lt;P&gt;ONE case statement assigns ONE value to ONE variable.&lt;/P&gt;
&lt;P&gt;So your code is apparently trying to violate that.&lt;/P&gt;
&lt;PRE&gt;CASE WHEN CD_ROLE = ' ' THEN  r.SOURCE 
ELSE CD_ROLE 
END AS CD_ROLE length = 20&lt;/PRE&gt;
&lt;P&gt;and a second case for the&lt;/P&gt;
&lt;PRE&gt;CASE WHEN CD_ROLE = ' ' THEN  r.NU_ROLE&lt;BR /&gt;else ' '  /* guessing as you did not provide an ELSE rule for NU_CLI*/
END AS NU_CLI length = 20&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 15:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Setting-a-column-value-with-an-existing-value-in-case/m-p/891743#M352251</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-30T15:11:20Z</dc:date>
    </item>
  </channel>
</rss>

