<?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: Case When Error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523114#M142096</link>
    <description>&lt;P&gt;please try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc SQL;
Create Table Current2 AS
Select *,
Case when Description eq ' ' then  'Unspecified'
else Description
end as Description
FROM Current1 ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 21 Dec 2018 17:06:21 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2018-12-21T17:06:21Z</dc:date>
    <item>
      <title>Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523112#M142095</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am hoping to replace the missing values in Description Variable with 'Unspecified'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help in identifying the cause of following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Proc SQL;&lt;BR /&gt;Create Table Current2 AS&lt;BR /&gt;Select *,&lt;BR /&gt;Case when Description eq ' ' then Description eq 'Unspecified'&lt;BR /&gt;else Description&lt;BR /&gt;end as Description&lt;BR /&gt;FROM Current1 ;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.S.Description variable is Character Type.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:03:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523112#M142095</guid>
      <dc:creator>VarunD</dc:creator>
      <dc:date>2018-12-21T17:03:10Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523114#M142096</link>
      <description>&lt;P&gt;please try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc SQL;
Create Table Current2 AS
Select *,
Case when Description eq ' ' then  'Unspecified'
else Description
end as Description
FROM Current1 ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523114#M142096</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-12-21T17:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523116#M142098</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Proc SQL;
Create Table Current2(drop=_description) AS
Select *,coalescec(_Description, 'Unspecified') as Description
FROM Current1(rename=(description=_description));
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Eventually there will be a day exclusive proc sql users will kill case when with ifc/ifn and coalesce c/n&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDITed to include a minor modification : rename and drop&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:34:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523116#M142098</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-21T17:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523117#M142099</link>
      <description>&lt;P&gt;using a datastep&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data current1;
infile cards missover;
input Description $11. ;
cards;
Unspecified
nspecified
specified
Unspecified

Unspecified
;
/* fails current2 */
Proc SQL;
Create Table Current2 AS
Select *,
Case when Description eq ' ' then Description eq 'Unspecified'
else Description eq Description
end as Description
FROM Current1 ;
QUIT;

data current3;
	set current1;
	if Description = '' then Description = 'Unspecified';
	else Description = Description;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Sometimes a datastep may be the tool to use.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523117#M142099</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-12-21T17:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523118#M142100</link>
      <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;I do not get an error now but the output data still has missing value.&lt;/P&gt;&lt;P&gt;It is not getting replaced by 'Unspecified'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:24:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523118#M142100</guid>
      <dc:creator>VarunD</dc:creator>
      <dc:date>2018-12-21T17:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523119#M142101</link>
      <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;I do not get an error now but the output data still has missing value.&lt;/P&gt;&lt;P&gt;It is not getting replaced by 'Unspecified'.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523119#M142101</guid>
      <dc:creator>VarunD</dc:creator>
      <dc:date>2018-12-21T17:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523120#M142102</link>
      <description>&lt;P&gt;Try a datastep like I have posted.&amp;nbsp; Sometimes a datastep is the tool to use&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:27:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523120#M142102</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-12-21T17:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523121#M142103</link>
      <description>&lt;P&gt;Thanks. This worked. But I don't understand why it won't work with Proc SQL.&lt;BR /&gt;Anyway. Appreciate your help.&lt;BR /&gt;Happy Holidays!&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:29:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523121#M142103</guid>
      <dc:creator>VarunD</dc:creator>
      <dc:date>2018-12-21T17:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523122#M142104</link>
      <description>&lt;P&gt;Using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122002"&gt;@VDD&lt;/a&gt;&amp;nbsp;sample&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data current1;
infile cards missover;
input Description $11. ;
cards;
Unspecified
nspecified
specified
Unspecified

Unspecified
;
Proc SQL;
Create Table Current2 AS
Select coalescec(Description, 'Unspecified') as Description
FROM Current1 ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523122#M142104</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-21T17:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523123#M142105</link>
      <description>Okay. This works when I only select the description variable. But when I do Select * , the missing values in description are still missing.</description>
      <pubDate>Fri, 21 Dec 2018 17:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523123#M142105</guid>
      <dc:creator>VarunD</dc:creator>
      <dc:date>2018-12-21T17:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523124#M142106</link>
      <description>&lt;P&gt;and there you have it you can not select the variable you are assigning how you were trying to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data current1;
infile cards missover;
input Description $11. ;
cards;
Unspecified
nspecified
specified
Unspecified

Unspecified
;
/* fails */
Proc SQL;
Create Table Current2 AS
Select *,
Case when Description eq ' ' then Description eq 'Unspecified'
else Description eq Description
end as Description
FROM Current1 ;
QUIT;
/* fails */
Proc SQL;
Create Table Current2a AS
Select *,
Case when Description eq ' ' then  'Unspecified'
else Description
end as Description
FROM Current1 ;
QUIT;
/* fails */
Proc SQL;
Create Table Current2b AS
Select *,coalescec(Description, 'Unspecified') as Description
FROM Current1 ;
QUIT;
/* works */
Proc SQL;
Create Table Current2c AS
Select coalescec(Description, 'Unspecified') as Description
FROM Current1 ;
QUIT;
/* works */
data current3;
	set current1;
	if Description = '' then Description = 'Unspecified';
	else Description = Description;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:36:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523124#M142106</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-12-21T17:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523125#M142107</link>
      <description>&lt;P&gt;Yes i noticed that and i made an edit to the previous post. here you go again. I made a mistake to post before sipping a coffee and hence I missed to noitce the *&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc SQL;
Create Table Current2(drop=_description) AS
Select *,coalescec(_Description, 'Unspecified') as Description
FROM Current1(rename=(description=_description));
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:36:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523125#M142107</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-21T17:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523126#M142108</link>
      <description>&lt;P&gt;This is Perfect !&lt;/P&gt;&lt;P&gt;Happy Holidays to you.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 17:40:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523126#M142108</guid>
      <dc:creator>VarunD</dc:creator>
      <dc:date>2018-12-21T17:40:18Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523131#M142112</link>
      <description>&lt;P&gt;Your WHEN clause has a numeric value (the result of the EQ operator) and your ELSE clause has the character variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table current2 as
  select *
       , case when description eq ' ' then 'unspecified'
              else description
         end as description
  from current1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Dec 2018 18:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523131#M142112</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-21T18:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: Case When Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523183#M142134</link>
      <description>&lt;P&gt;And an approach using a FORMAT which would avoid the conditional entirely.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value $junk
' '='Unspecified';
;
run;

data junk;
  input x $10.;
  y= put(x,$junk.);
datalines;
valid 
invalid
something
.
anything
;
run;

proc sql;
   create table junk2
   as select put(x,$junk.)
   from junk;
quit;&lt;/PRE&gt;
&lt;P&gt;This would have one advantage that it should work with nearly any character variable with missing values though in some circumstances such as a data step where the declared variable length isn't long enough to hold "Undescribed" it will get truncated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Dec 2018 20:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-When-Error/m-p/523183#M142134</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-12-21T20:49:58Z</dc:date>
    </item>
  </channel>
</rss>

