<?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 If..Then..Else Statements in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29318#M4035</link>
    <description>I hope this is reaching you in the best of health and spirits&lt;BR /&gt;
&lt;BR /&gt;
I am new to EG and still trying to feel my way around. Is there a way to expand the use of the compute function in the query task? I would like to define a column's value base of data from tw different fields:&lt;BR /&gt;
&lt;BR /&gt;
Example&lt;BR /&gt;
If Sub-Market Code = 500 and Market is = "Test" then name "Test Group"&lt;BR /&gt;
&lt;BR /&gt;
If this is not possible with the compute function in the query task can you advice me how to do an then..else statement in EG&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance</description>
    <pubDate>Mon, 07 Jul 2008 20:40:10 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-07-07T20:40:10Z</dc:date>
    <item>
      <title>If..Then..Else Statements</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29318#M4035</link>
      <description>I hope this is reaching you in the best of health and spirits&lt;BR /&gt;
&lt;BR /&gt;
I am new to EG and still trying to feel my way around. Is there a way to expand the use of the compute function in the query task? I would like to define a column's value base of data from tw different fields:&lt;BR /&gt;
&lt;BR /&gt;
Example&lt;BR /&gt;
If Sub-Market Code = 500 and Market is = "Test" then name "Test Group"&lt;BR /&gt;
&lt;BR /&gt;
If this is not possible with the compute function in the query task can you advice me how to do an then..else statement in EG&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance</description>
      <pubDate>Mon, 07 Jul 2008 20:40:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29318#M4035</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-07-07T20:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: If..Then..Else Statements</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29319#M4036</link>
      <description>You can get this done with the CASE statement in the expression builder in the query builder.  If you are using the query to pull a subset of the data, this has the advantage of creating the new column as it filters the data.&lt;BR /&gt;
&lt;BR /&gt;
Create a calculated column, using an expression like this:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
CASE  &lt;BR /&gt;
  WHEN (SOURCE.sub_market_code = 500 and SOURCE.market = "Test") &lt;BR /&gt;
    THEN "Test Group"  &lt;BR /&gt;
END&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
EG will generate the "as Name" suffix that creates a new column named Name or whatever you choose.&lt;BR /&gt;
&lt;BR /&gt;
That works within the proc sql that EG generates.&lt;BR /&gt;
&lt;BR /&gt;
If want a code approach and don't mind making another pass through the data, you could use a simple DATA step:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data SOURCE;&lt;BR /&gt;
set SOURCE;&lt;BR /&gt;
length name $ 20;&lt;BR /&gt;
if (DATA.sub_market_code = 500 and DATA.market = "Test")  then&lt;BR /&gt;
  name="Test Group";&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Chris</description>
      <pubDate>Mon, 07 Jul 2008 23:27:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29319#M4036</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2008-07-07T23:27:58Z</dc:date>
    </item>
    <item>
      <title>Re: If..Then..Else Statements</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29320#M4037</link>
      <description>Hi:&lt;BR /&gt;
  In the GUI interface, when you create a computed column, EG gives you an expression editor where you can enter your "condition". The "new computed column" is entered through the popup window that's associated with the expression. EG is actually building an SQL query behind the scenes and creating PROC SQL code.&lt;BR /&gt;
 &lt;BR /&gt;
  The way you create columns conditionally in PROC SQL is with a CASE clause. For example:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   create table work.newclass as &lt;BR /&gt;
   select a.Name,&lt;BR /&gt;
	  a.Sex,&lt;BR /&gt;
	  a.Age,&lt;BR /&gt;
     (case&lt;BR /&gt;
        when a.sex = 'F' and a.age lt 13 then 'One'&lt;BR /&gt;
        when a.sex = 'F' and a.age ge 13 then 'Two'&lt;BR /&gt;
        when a.sex = 'M' and a.age lt 14 then 'Three'&lt;BR /&gt;
        when a.sex = 'M' and a.age ge 14 then 'Four'&lt;BR /&gt;
        else 'None'&lt;BR /&gt;
     end) as newvar &lt;BR /&gt;
 from sashelp.class as a;&lt;BR /&gt;
quit;&lt;BR /&gt;
     &lt;BR /&gt;
proc print data=newclass;&lt;BR /&gt;
  title 'SQL CASE Example ';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
               &lt;BR /&gt;
So, if you build your query with the EG expression editor, I believe (and you might want to check with Tech Support on this) that you have to enter just the (CASE/END) in the expression editor. EG adds the "AS Calculation1" or whatever you call the new column in the computed column window.&lt;BR /&gt;
&lt;BR /&gt;
In your example, the CASE clause would be something like:&lt;BR /&gt;
[pre]&lt;BR /&gt;
(case&lt;BR /&gt;
   when Sub_Market = 500 and Market = 'Test' then "Test Group"&lt;BR /&gt;
   when Sub_Market = 250 and Market = 'Prod' then "Not Test"&lt;BR /&gt;
  ... more conditions ...&lt;BR /&gt;
   else "Other"&lt;BR /&gt;
end)&lt;BR /&gt;
[/pre]  &lt;BR /&gt;
                            &lt;BR /&gt;
You normally have to use the column NAMES and not the column LABELS in your expressions. The string "Sub-Market Code" is probably the label for a column and is not a column name because SAS columns cannot contain spaces or dashes. I don't know what the variable name is, but I coded it as Sub_Market in the code snippet above.&lt;BR /&gt;
 &lt;BR /&gt;
If you have not already found them, there are 2 excellent books on using SAS EG:&lt;BR /&gt;
The Little SAS Book for Enterprise Guide 4.1: For Enterprise Guide 4.1 &lt;BR /&gt;
By Susan J. Slaughter, Lora D. Delwiche &lt;BR /&gt;
and&lt;BR /&gt;
SAS for Dummies&lt;BR /&gt;
by Stephen McDaniel, Chris Hemedinger&lt;BR /&gt;
&lt;BR /&gt;
The second book goes into more detail on the SAS Business Intelligence Platform, so it covers more topics than the first book. I'd recommend checking them both out and deciding which one better fits your needs.&lt;BR /&gt;
&lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 08 Jul 2008 03:45:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29320#M4037</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-07-08T03:45:00Z</dc:date>
    </item>
    <item>
      <title>Re: If..Then..Else Statements</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29321#M4038</link>
      <description>Thank you</description>
      <pubDate>Wed, 09 Jul 2008 19:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/If-Then-Else-Statements/m-p/29321#M4038</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-07-09T19:25:52Z</dc:date>
    </item>
  </channel>
</rss>

