<?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 PROC SQL  - Creating New Column in Existing Table with conditional logic in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61847#M17543</link>
    <description>I'm trying to use PROC SQL to create a new column in an existing table.  I can't seem to get my "CASE", "WHEN" expression syntax to work.  I'm creating a new table, from columns of two existing tables (table aliases a and b) using a LEFT JOIN.  Basically, this new column is to be used as a flag, like "YES" or "NO", based on values of another column already in table b.&lt;BR /&gt;
&lt;BR /&gt;
An example using IF,THEN statement in conventional SAS programming is:&lt;BR /&gt;
&lt;BR /&gt;
Data Loan_Categories (keep=Category Purpose_Code Loan_Amount Loan_Terms);&lt;BR /&gt;
Length Category $ 8 ;&lt;BR /&gt;
     set Loan_data (keep= Purpose_Code  Loan_Amount Loan_Terms) ;&lt;BR /&gt;
X=Purpose_Code;&lt;BR /&gt;
IF  X &amp;gt;= 2 THEN Category = "MORTGAGE";&lt;BR /&gt;
ELSE &lt;BR /&gt;
IF X = 1 THEN Category = "CAR";&lt;BR /&gt;
ELSE&lt;BR /&gt;
IF X &amp;lt; 1 THEN Category = "BUSINESS";&lt;BR /&gt;
ELSE &lt;BR /&gt;
Category = "UNKNOWN";&lt;BR /&gt;
run;</description>
    <pubDate>Wed, 24 Feb 2010 16:19:43 GMT</pubDate>
    <dc:creator>chandler</dc:creator>
    <dc:date>2010-02-24T16:19:43Z</dc:date>
    <item>
      <title>PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61847#M17543</link>
      <description>I'm trying to use PROC SQL to create a new column in an existing table.  I can't seem to get my "CASE", "WHEN" expression syntax to work.  I'm creating a new table, from columns of two existing tables (table aliases a and b) using a LEFT JOIN.  Basically, this new column is to be used as a flag, like "YES" or "NO", based on values of another column already in table b.&lt;BR /&gt;
&lt;BR /&gt;
An example using IF,THEN statement in conventional SAS programming is:&lt;BR /&gt;
&lt;BR /&gt;
Data Loan_Categories (keep=Category Purpose_Code Loan_Amount Loan_Terms);&lt;BR /&gt;
Length Category $ 8 ;&lt;BR /&gt;
     set Loan_data (keep= Purpose_Code  Loan_Amount Loan_Terms) ;&lt;BR /&gt;
X=Purpose_Code;&lt;BR /&gt;
IF  X &amp;gt;= 2 THEN Category = "MORTGAGE";&lt;BR /&gt;
ELSE &lt;BR /&gt;
IF X = 1 THEN Category = "CAR";&lt;BR /&gt;
ELSE&lt;BR /&gt;
IF X &amp;lt; 1 THEN Category = "BUSINESS";&lt;BR /&gt;
ELSE &lt;BR /&gt;
Category = "UNKNOWN";&lt;BR /&gt;
run;</description>
      <pubDate>Wed, 24 Feb 2010 16:19:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61847#M17543</guid>
      <dc:creator>chandler</dc:creator>
      <dc:date>2010-02-24T16:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61848#M17544</link>
      <description>Suggest you share whatever SAS PROC SQL code you have tried which doesn't work for direct/concise feedback.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Recommended Google advanced search argument, this topic/post:&lt;BR /&gt;
&lt;BR /&gt;
proc sql add new column site:sas.com</description>
      <pubDate>Wed, 24 Feb 2010 16:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61848#M17544</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-02-24T16:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61849#M17545</link>
      <description>CASE&lt;BR /&gt;
WHEN Purpose_Code &amp;gt;= 2 THEN  "MORTGAGE"&lt;BR /&gt;
WHEN Purpose_Code = 1 THEN  "CAR"&lt;BR /&gt;
WHEN Purpose_Code &amp;lt; 1 THEN  "BUSINESS"&lt;BR /&gt;
ELSE &lt;BR /&gt;
"UNKNOWN" END as category&lt;BR /&gt;
&lt;BR /&gt;
Should do it</description>
      <pubDate>Wed, 24 Feb 2010 16:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61849#M17545</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2010-02-24T16:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61850#M17546</link>
      <description>proc sql noexec;&lt;BR /&gt;
   create table prodlib.Loan_System_PCS_xref as&lt;BR /&gt;
    select application_id, obligor, obligation&lt;BR /&gt;
	from whse.afs_w_acaps_all as a LEFT JOIN prodlib.regH_suspects_newcolumns as b&lt;BR /&gt;
	on a.application_id=b.application_id &lt;BR /&gt;
	CASE when SCAN(obligor,1) gt ' '&lt;BR /&gt;
     then SYSTEM = "AFS_Level3"&lt;BR /&gt;
       else SYSTEM = ' '&lt;BR /&gt;
    end as SYSTEM; &lt;BR /&gt;
quit;</description>
      <pubDate>Wed, 24 Feb 2010 16:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61850#M17546</guid>
      <dc:creator>chandler</dc:creator>
      <dc:date>2010-02-24T16:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61851#M17547</link>
      <description>Thanks for your quick reply, FLIP.  I will try this, now.</description>
      <pubDate>Wed, 24 Feb 2010 16:47:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61851#M17547</guid>
      <dc:creator>chandler</dc:creator>
      <dc:date>2010-02-24T16:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61852#M17548</link>
      <description>The CASE needs to be part of the SELECT clause, not after the JOIN.&lt;BR /&gt;
&lt;BR /&gt;
proc sql noexec;&lt;BR /&gt;
create table prodlib.Loan_System_PCS_xref as&lt;BR /&gt;
select application_id, obligor, obligation,&lt;BR /&gt;
CASE when SCAN(obligor,1) gt ' '&lt;BR /&gt;
then SYSTEM = "AFS_Level3"&lt;BR /&gt;
else SYSTEM = ' '&lt;BR /&gt;
end as SYSTEM&lt;BR /&gt;
from whse.afs_w_acaps_all as a LEFT JOIN prodlib.regH_suspects_newcolumns as b&lt;BR /&gt;
on a.application_id=b.application_id&lt;BR /&gt;
;&lt;BR /&gt;
quit;</description>
      <pubDate>Wed, 24 Feb 2010 16:47:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61852#M17548</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2010-02-24T16:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61853#M17549</link>
      <description>How about this suggested DOC reference?  Did it help with your PROC SQL coding?  What type of error are you getting with your particular environment and data conditions?  It would be helpful to see your SAS log information with any errors, for an accurate diagnosis and helpful feedback.&lt;BR /&gt;
&lt;BR /&gt;
For your consideration and use/review, I have provided a very specific DOC link - one that provides CASE coding syntax and examples.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/a001334113.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/a001334113.htm&lt;/A&gt;</description>
      <pubDate>Wed, 24 Feb 2010 16:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61853#M17549</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-02-24T16:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61854#M17550</link>
      <description>In addition to what Doc said, dont put the SYSTEM = in the THEN clause.  The assignment is handled after the end as clause, not as part of the value generation.</description>
      <pubDate>Wed, 24 Feb 2010 16:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61854#M17550</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2010-02-24T16:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61855#M17551</link>
      <description>I'm running SAS version 9.1.3 with Service Pack 4 on a Windows XP Professional platform.  Here is my SAS Log below.  &lt;BR /&gt;
&lt;BR /&gt;
1981  proc sql noexec;&lt;BR /&gt;
1982  create table prodlib.Loan_System_PCS_xref as&lt;BR /&gt;
1983  select application_id, obligor, obligation&lt;BR /&gt;
1984  from whse.afs_w_acaps_all as a LEFT JOIN prodlib.regH_suspects_newcolumns as b&lt;BR /&gt;
1985  on a.application_id=b.application_id&lt;BR /&gt;
1986  CASE when SCAN(obligor,1) gt ' '&lt;BR /&gt;
      ----&lt;BR /&gt;
      22&lt;BR /&gt;
      76&lt;BR /&gt;
ERROR 22-322: Syntax error, expecting one of the following: ;, !, !!, &amp;amp;, *, **, +, ',', -, /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, AND,&lt;BR /&gt;
              ANSIMISS, CROSS, EQ, EQT, EXCEPT, FULL, GE, GET, GROUP, GT, GTT, HAVING, INNER, INTERSECT, JOIN, LE, LEFT, LET,&lt;BR /&gt;
              LT, LTT, NATURAL, NE, NET, NOMISS, NOT, OR, ORDER, OUTER, RIGHT, UNION, WHERE, ^, ^=, |, ||, ~, ~=.&lt;BR /&gt;
&lt;BR /&gt;
ERROR 76-322: Syntax error, statement will be ignored.&lt;BR /&gt;
&lt;BR /&gt;
1987  then SYSTEM = "AFS_Level3"&lt;BR /&gt;
1988  else SYSTEM = ' '&lt;BR /&gt;
1989  end as SYSTEM;&lt;BR /&gt;
1990  quit;&lt;BR /&gt;
NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;
NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;
      real time           0.01 seconds&lt;BR /&gt;
      cpu time            0.00 seconds</description>
      <pubDate>Wed, 24 Feb 2010 20:40:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61855#M17551</guid>
      <dc:creator>chandler</dc:creator>
      <dc:date>2010-02-24T20:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL  - Creating New Column in Existing Table with conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61856#M17552</link>
      <description>Re-Read Doc's comment , then move the case into the select clause.&lt;BR /&gt;
&lt;BR /&gt;
proc sql noexec;&lt;BR /&gt;
 create table prodlib.Loan_System_PCS_xref as&lt;BR /&gt;
 select application_id, obligor, obligation,&lt;BR /&gt;
&lt;BR /&gt;
CASE when SCAN(obligor,1) gt ' '&lt;BR /&gt;
ELSE&lt;BR /&gt;
END AS ....&lt;BR /&gt;
 from whse.afs_w_acaps_all as a LEFT JOIN prodlib.regH_suspects_newcolumns as b&lt;BR /&gt;
 on a.application_id=b.application_id</description>
      <pubDate>Wed, 24 Feb 2010 20:43:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Creating-New-Column-in-Existing-Table-with-conditional/m-p/61856#M17552</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2010-02-24T20:43:16Z</dc:date>
    </item>
  </channel>
</rss>

