<?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 Checking for Existence of a Variable...Then Action Based on Result in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594486#M170801</link>
    <description>&lt;P&gt;I have the following code that checks for air traffic in markets:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE EGTASK.OVR_WCU_MKTS_FINAL AS 
   SELECT t1.FltDate, 
          (t1.curr_mkt_11 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_11, 
          (t1.curr_mkt_14 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_14, 
          (t1.curr_mkt_22 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_22, 
          (t1.curr_mkt_23 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_23, 
          (t1.curr_mkt_24 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_24, 
          (t1.curr_mkt_30 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_30, 
          /* curr_mkt_60 */
            (t1.curr_mkt_60 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_60, 

            (t2.prev_mkt_11 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_11, 
            (t2.prev_mkt_14 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_14, 
            (t2.prev_mkt_22 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_22, 
            (t2.prev_mkt_23 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_23, 
            (t2.prev_mkt_24 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_24, 
            (t2.prev_mkt_30 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_30, 
          /* prev_mkt_60 */
            (t2.prev_mkt_60 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_60&lt;BR /&gt;
      FROM EGTASK.OVR_CURR_MKT_FINAL t1
           INNER JOIN EGTASK.OVR_PREV_MKT_FINAL t2 ON (t1.FltDate = t2.FltDate)
      ORDER BY t1.FltDate;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The problem curr references the current year and prev references previous year.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Market 60 is the "Other Market" and the issue is that it may not exist for a given time frame. As of today (October 7th), there are no flights either in the current year or the previous year for this market, so the SQL will bomb for the market 60 calculations above since there are no 60 variables in the source tables. How would I check to see if they exist in the source data (t1 or t2 above) and if they don't exist, set them equal to 0.&amp;nbsp; That is, create curr_mkt_60 and/or prev_mkt_60 and set equal to 0 if needed.&lt;/P&gt;</description>
    <pubDate>Mon, 07 Oct 2019 16:06:40 GMT</pubDate>
    <dc:creator>BCNAV</dc:creator>
    <dc:date>2019-10-07T16:06:40Z</dc:date>
    <item>
      <title>Checking for Existence of a Variable...Then Action Based on Result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594486#M170801</link>
      <description>&lt;P&gt;I have the following code that checks for air traffic in markets:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE EGTASK.OVR_WCU_MKTS_FINAL AS 
   SELECT t1.FltDate, 
          (t1.curr_mkt_11 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_11, 
          (t1.curr_mkt_14 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_14, 
          (t1.curr_mkt_22 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_22, 
          (t1.curr_mkt_23 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_23, 
          (t1.curr_mkt_24 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_24, 
          (t1.curr_mkt_30 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_30, 
          /* curr_mkt_60 */
            (t1.curr_mkt_60 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_mkt_60, 

            (t2.prev_mkt_11 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_11, 
            (t2.prev_mkt_14 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_14, 
            (t2.prev_mkt_22 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_22, 
            (t2.prev_mkt_23 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_23, 
            (t2.prev_mkt_24 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_24, 
            (t2.prev_mkt_30 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_30, 
          /* prev_mkt_60 */
            (t2.prev_mkt_60 * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_mkt_60&lt;BR /&gt;
      FROM EGTASK.OVR_CURR_MKT_FINAL t1
           INNER JOIN EGTASK.OVR_PREV_MKT_FINAL t2 ON (t1.FltDate = t2.FltDate)
      ORDER BY t1.FltDate;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The problem curr references the current year and prev references previous year.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Market 60 is the "Other Market" and the issue is that it may not exist for a given time frame. As of today (October 7th), there are no flights either in the current year or the previous year for this market, so the SQL will bomb for the market 60 calculations above since there are no 60 variables in the source tables. How would I check to see if they exist in the source data (t1 or t2 above) and if they don't exist, set them equal to 0.&amp;nbsp; That is, create curr_mkt_60 and/or prev_mkt_60 and set equal to 0 if needed.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 16:06:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594486#M170801</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2019-10-07T16:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Checking for Existence of a Variable...Then Action Based on Result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594502#M170813</link>
      <description>&lt;P&gt;Add two new steps before your sql code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tmp_ovr_curr_mkt;
       format curr_mkt_11 curr_mkt_14 ... /* full list of output variables */ comma2.0;
  set egtask.ovr_curr_mkt_final;&lt;BR /&gt;       keep &amp;lt;list of only need variables &amp;gt; ;
run;
              
data tmp_ovr_prev_mkt;
       format curr_mkt_11 curr_mkt_14 ... /* full list of output variables */ comma2.0;
  set egtask.ovr_prev_mkt_final;&lt;BR /&gt;      keep &amp;lt;list of only need variables &amp;gt; ;
run;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;thus you have created all variables to exist, ahead.&lt;/P&gt;
&lt;P&gt;use the tmp_ datasets instead the original.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 17:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594502#M170813</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-10-07T17:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: Checking for Existence of a Variable...Then Action Based on Result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594551#M170835</link>
      <description>&lt;P&gt;This&amp;nbsp; sort of issue is one of the reasons data should not be in variable names. If your data were structured to have a MARKET variable then joining on the market variable along with the date would reduce this to something like:&lt;/P&gt;
&lt;PRE&gt;PROC SQL;
   CREATE TABLE EGTASK.OVR_WCU_MKTS_FINAL AS 
   SELECT t1.FltDate,market, curr 
          (t1.curr_mkt * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS curr_, 
          (t2.prev_mkt * &amp;amp;br_ovr) FORMAT=COMMA20.0 AS prev_, 
      FROM EGTASK.OVR_CURR_MKT_FINAL t1
           INNER JOIN EGTASK.OVR_PREV_MKT_FINAL t2 
          ON (t1.FltDate = t2.FltDate and t1.market=t2.market)
      ORDER BY t1.FltDate;
QUIT;&lt;/PRE&gt;
&lt;P&gt;Which wouldn't have issues that might arise when you have some other market temporarily out of the data, such as an airline strike or extended weather event, or when a new market is added (Kamchatka anyone)&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 20:44:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594551#M170835</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-07T20:44:01Z</dc:date>
    </item>
    <item>
      <title>Re: Checking for Existence of a Variable...Then Action Based on Result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594554#M170836</link>
      <description>&lt;P&gt;You could create a view:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _V_ALL_COLUMNS/view=_V_ALL_COLUMNS;
  retain CURR_MKT_60 PREV_MKT: PREV_MKT_60 0;
  set EGTASK.OVR_PREV_MKT_FINAL;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and query that view.&lt;/P&gt;
&lt;P&gt;If you want the columns at the end, you can use a format statement as per &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;, or something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _V_ALL_COLUMNS/view=_V_ALL_COLUMNS;
  if 0 then set EGTASK.OVR_PREV_MKT_FINAL;
  retain CURR_MKT_60 PREV_MKT: PREV_MKT_60 0;
  set EGTASK.OVR_PREV_MKT_FINAL;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 20:48:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594554#M170836</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-10-07T20:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: Checking for Existence of a Variable...Then Action Based on Result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594760#M170960</link>
      <description>&lt;P&gt;Yes...having better data is always good, but we don't always have control over that. The first solution is simple and elegant....I always know what markets exist, just not if there is data or not!&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 13:46:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-for-Existence-of-a-Variable-Then-Action-Based-on-Result/m-p/594760#M170960</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2019-10-08T13:46:42Z</dc:date>
    </item>
  </channel>
</rss>

