<?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: using case when to create new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432120#M106969</link>
    <description>&lt;P&gt;This is the correct way of doing it. Hope it helps.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;proc sql;

create table transactions as select a.*,

(case when b.type='AB' then b.Amount end) as loan,

(case when b.type='AC' then b.Amount end) as debit

from set_one a

left join

 set_two  b

on a.ID=b.ID;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 30 Jan 2018 09:35:17 GMT</pubDate>
    <dc:creator>Satish_Parida</dc:creator>
    <dc:date>2018-01-30T09:35:17Z</dc:date>
    <item>
      <title>using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432118#M106968</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have two data sets&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;set one&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Type Amount&lt;/P&gt;&lt;P&gt;1&amp;nbsp; AB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1000&lt;/P&gt;&lt;P&gt;2&amp;nbsp; AC&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2000&lt;/P&gt;&lt;P&gt;3&amp;nbsp; AD&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3000&lt;/P&gt;&lt;P&gt;4&amp;nbsp; AF&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4000&lt;/P&gt;&lt;P&gt;5&amp;nbsp; AE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;set two&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the first data set, observations in variable "type" are a symbol for example AB stands for "loan".&lt;/P&gt;&lt;P&gt;Now i want to join these sets on ID and create a new variable based on a&amp;nbsp;type variable from set one.&lt;/P&gt;&lt;P&gt;example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Loan&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 1000&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried to do it with case when&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;create&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;transactions &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt; &lt;FONT color="#008080" face="Courier New" size="3"&gt;a.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;*, &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;(&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;case&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; b.type=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'AB'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; b.Amount &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; loan&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;set_one a&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;left&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;join&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;set_two &amp;nbsp;b&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;on&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a.ID=b.ID;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;and it works, but&amp;nbsp;I want it to be more efficient and&amp;nbsp;added more "when"&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;create&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;transactions &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt; &lt;FONT color="#008080" face="Courier New" size="3"&gt;a.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;*, &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;(&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;case&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; b.type=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'AB'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; b.Amount &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; loan&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New"&gt;when&lt;/FONT&gt; b.type=&lt;FONT color="#800080" face="Courier New" size="3"&gt;'AC'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; b.Amount &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;) &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt; debit&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;set_one a&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;left&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;join&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;set_two &amp;nbsp;b&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;on&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a.ID=b.ID;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;it gave me a syntax error, probably cause the end statement in loan step.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Is there another way to solve this problem, how can&amp;nbsp;I join these tables ?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 09:30:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432118#M106968</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2018-01-30T09:30:21Z</dc:date>
    </item>
    <item>
      <title>Re: using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432120#M106969</link>
      <description>&lt;P&gt;This is the correct way of doing it. Hope it helps.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;proc sql;

create table transactions as select a.*,

(case when b.type='AB' then b.Amount end) as loan,

(case when b.type='AC' then b.Amount end) as debit

from set_one a

left join

 set_two  b

on a.ID=b.ID;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jan 2018 09:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432120#M106969</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2018-01-30T09:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432121#M106970</link>
      <description>&lt;P&gt;Each case must have its own end, and each end must have a case. You can't have one case with two ends.&lt;/P&gt;
&lt;P&gt;The brackets around the case - end blocks are not necessary&lt;/P&gt;
&lt;P&gt;For clarity, write it like that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case
  when (condition) then ....
  when (condition) then ...
  else ...
end as newvar&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jan 2018 09:37:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432121#M106970</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-30T09:37:26Z</dc:date>
    </item>
    <item>
      <title>Re: using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432122#M106971</link>
      <description>&lt;P&gt;&amp;nbsp;it doesn't, tried this also&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;_ ____ _&lt;/P&gt;&lt;P&gt;22 201 76&lt;/P&gt;&lt;P&gt;ERROR 22-322: Syntax error, expected: ',', FROM.&lt;/P&gt;&lt;P&gt;ERROR 201-322: The option is not recognized and will be ignored.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 09:38:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432122#M106971</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2018-01-30T09:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432123#M106972</link>
      <description>&lt;P&gt;Use the code window (its the {i} above post area) when posting code:&lt;/P&gt;
&lt;PRE&gt;proc sql; 
  create table transactions as 
  select a.*,
         case when b.type='AB' then b.Amount else . end as loan,
         case when b.type='AC' then b.Amount else . end as debit 
  from   set_one a
  left join  set_two  b
  on     a.id=b.id;
quit;&lt;/PRE&gt;
&lt;P&gt;So to create a variable it is one line on its own, with its own case/when/end block.&amp;nbsp; Do note you don't need all the extra brackets and what not.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 09:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432123#M106972</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-30T09:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432124#M106973</link>
      <description>ok, i've changed that every case will have it own end like Satish wrote and i have an error.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;&lt;BR /&gt;create table transactions as select a.*,&lt;BR /&gt;&lt;BR /&gt;(case when b.type='AB' then b.Amount end) as loan,&lt;BR /&gt;&lt;BR /&gt;(case when b.type='AC' then b.Amount end) as debit&lt;BR /&gt;&lt;BR /&gt;from set_one a&lt;BR /&gt;&lt;BR /&gt;left join&lt;BR /&gt;&lt;BR /&gt;set_two b&lt;BR /&gt;&lt;BR /&gt;on a.ID=b.ID;&lt;BR /&gt;&lt;BR /&gt;quit;</description>
      <pubDate>Tue, 30 Jan 2018 09:40:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432124#M106973</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2018-01-30T09:40:12Z</dc:date>
    </item>
    <item>
      <title>Re: using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432125#M106974</link>
      <description>It missed a comma in line4, please try now</description>
      <pubDate>Tue, 30 Jan 2018 09:40:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432125#M106974</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2018-01-30T09:40:37Z</dc:date>
    </item>
    <item>
      <title>Re: using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432126#M106975</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/107827"&gt;@Jedrzej&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;it doesn't, tried this also&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;_ ____ _&lt;/P&gt;
&lt;P&gt;22 201 76&lt;/P&gt;
&lt;P&gt;ERROR 22-322: Syntax error, expected: ',', FROM.&lt;/P&gt;
&lt;P&gt;ERROR 201-322: The option is not recognized and will be ignored.&lt;/P&gt;
&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When posting an ERROR, &lt;STRONG&gt;ALWAYS&lt;/STRONG&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;post the &lt;STRONG&gt;&lt;EM&gt;whole&lt;/EM&gt;&lt;/STRONG&gt; log of the failed step&lt;/LI&gt;
&lt;LI&gt;use the {i} button, so that the formatting of the log is preserved; this is crucial in identifying the offending codepiece&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Tue, 30 Jan 2018 09:41:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432126#M106975</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-30T09:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: using case when to create new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432127#M106976</link>
      <description>works, thx</description>
      <pubDate>Tue, 30 Jan 2018 09:43:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-case-when-to-create-new-variable/m-p/432127#M106976</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2018-01-30T09:43:05Z</dc:date>
    </item>
  </channel>
</rss>

