<?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: Validate if a variable exists and then select or implement a case if in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/475556#M122304</link>
    <description>&lt;P&gt;First of all, I see the problem is you are trying to execute Proc Sql within from %test macro within proc sql in following steps.&lt;/P&gt;&lt;P&gt;You need to only consider select statement.Please check first.&lt;/P&gt;</description>
    <pubDate>Thu, 05 Jul 2018 10:14:14 GMT</pubDate>
    <dc:creator>Pankp</dc:creator>
    <dc:date>2018-07-05T10:14:14Z</dc:date>
    <item>
      <title>Validate if a variable exists and then select or implement a case if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/298373#M62740</link>
      <description>&lt;P&gt;Hi All dears&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm in the need to check if within a dataset a specif column exists, and depending on the existance, selecting the variable or execute a case if step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The check must be used within a proc sql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had the idea of doing the checks in &amp;nbsp;Macros ( first macro to check the existance, second macro including the two action depending on the presence or not of the variable), and then utilize the same macro in the final &amp;nbsp;proc sql to define a variable related to the specifc variable/month&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the idea would be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;1. Macro - checking or not the existance of the column

LIBNAME EC1 "/intl/iimis4/prod/cgf/emea/spain";

%let bom=200;

%macro varexist
/*----------------------------------------------------------------------
Check for the existence of a specified variable.
----------------------------------------------------------------------*/
(ds   =   ec1.cust_seg 
,var  =  cust_seg_mth&amp;amp;bom
);
/*----------------------------------------------------------------------
Usage Notes:
%if %varexist(&amp;amp;data,NAME)
  %then %put input data set contains variable NAME;
The macro calls resolves to 0 when either the data set does not exist
or the variable is not in the specified data set.
----------------------------------------------------------------------*/
%local dsid rc ;
%*----------------------------------------------------------------------
Use SYSFUNC to execute OPEN, VARNUM, and CLOSE functions.
-----------------------------------------------------------------------;
%let dsid = %sysfunc(open(&amp;amp;ds));
%if (&amp;amp;dsid) %then %do;
  %if %sysfunc(varnum(&amp;amp;dsid,&amp;amp;var)) %then 1;
  %else 0 ;
  %let rc = %sysfunc(close(&amp;amp;dsid));
%end;
%else 0;
%mend varexist;


2. Macro - dependign on the existance or not select one of the two option (1 select the variable / 2 execute the case if)


%macro&amp;nbsp;test;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;%if&amp;nbsp;%varexist =&amp;nbsp;1&amp;nbsp;%then&amp;nbsp;%do;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc sql ;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select cust_seg_mth&amp;amp;eom from ec1.cust_seg; quit; &amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;%end;&amp;nbsp;%else&amp;nbsp;%do;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;proc sql;
select
(case when (intck('month',t2.cust_estab_dt, '31aug2016'd )&amp;gt;=12 and cust_rel_ind&amp;amp;eom=1) then "Organic"
 when (intck('month',t2.cust_estab_dt, '31aug2016'd)&amp;gt;=12 and cust_rel_ind&amp;amp;eom=0) then "Attrition"
when intck('month',t2.cust_estab_dt, '31aug2016'd)&amp;lt;=11 then "Acquired" end) 
 from ec1.cust_seg; 
quit;

&amp;nbsp;&amp;nbsp;&amp;nbsp;%end;&amp;nbsp;
%mend&amp;nbsp;test;&amp;nbsp;


3. Recall the macro in the fianl proc sql;


proc sql;
create table histcust as 
select t1.acct11, t2.cust_estab_dt,
cust_seg_mth181 as jan2015,
cust_seg_mth182 as feb2015,
cust_seg_mth183 as mar2015,
cust_seg_mth184 as apr2015,
cust_seg_mth185 as may2015,
cust_seg_mth186 as jun2015,
cust_seg_mth187 as jul2015,
cust_seg_mth188 as aug2015,
cust_seg_mth189 as sep2015,
cust_seg_mth190 as oct2015,
cust_seg_mth191 as nov2015,
cust_seg_mth192 as dec2015,
cust_seg_mth193 as jan2016,
cust_seg_mth194 as feb2016,
cust_seg_mth195 as mar2016,
cust_seg_mth196 as apr2016,
cust_seg_mth197 as may2016,
cust_seg_mth198 as jun2016,
cust_seg_mth199 as jul2016,
%test  as aug2016


from  ec1.acct_attr t1 left join ec1.cust_seg t2 on t1.acct11=t2.acct11
left join ec1.cust_attr t3 on t1.amex_cust_id=t3.amex_cust_id

order by t1.acct11, t1.amex_cust_id
 ;
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, I'm not a macro user so I have tried something but getting as natural error&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I suppose the first macro give a 1 or 0 depending on the variable presence but not sure on the second one&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second macro should be supposed to give a specific value (the corresponding variable record or the result of the case if), related to each ID (acct11) of each record derived in the final proc sql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyone could help ??&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for the precious support&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bests&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 16:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/298373#M62740</guid>
      <dc:creator>dcortell</dc:creator>
      <dc:date>2016-09-14T16:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: Validate if a variable exists and then select or implement a case if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/298387#M62746</link>
      <description>&lt;P&gt;When you get an error message please post the appropriate section of the Log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect you have at least few things going on.&lt;/P&gt;
&lt;P&gt;First you attempt to use the macro with this code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;%if&amp;nbsp;%varexist =&amp;nbsp;1&amp;nbsp;%then&amp;nbsp;%do;&lt;/P&gt;
&lt;P&gt;Your default value for VAR involved a macro variable in defining the value for the VAR parameter (a not optimal approach) so the value of &amp;amp;BOM at time of compiling is the default and may not match the value you expect when not actually using the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your sql code to execute when the varexist is true has&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select cust_seg_mth&amp;amp;eom&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but the variable tested for was cust_seg_mth&amp;amp;&lt;STRONG&gt;b&lt;/STRONG&gt;om&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the results you should provide a few records of the input variables of interest and what you expect for the output as it is not obvious what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second is likely to be Scope&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 17:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/298387#M62746</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-14T17:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: Validate if a variable exists and then select or implement a case if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/298393#M62751</link>
      <description>&lt;P&gt;In your second macro you&amp;nbsp;didn't tell the %VAREXIST macro what variable to check for. So the result will always be 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;The macro calls resolves to 0 when either the data set does not exist
or the variable is not in the specified data set.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Sep 2016 17:47:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/298393#M62751</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-09-14T17:47:56Z</dc:date>
    </item>
    <item>
      <title>Re: Validate if a variable exists and then select or implement a case if</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/475556#M122304</link>
      <description>&lt;P&gt;First of all, I see the problem is you are trying to execute Proc Sql within from %test macro within proc sql in following steps.&lt;/P&gt;&lt;P&gt;You need to only consider select statement.Please check first.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 10:14:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Validate-if-a-variable-exists-and-then-select-or-implement-a/m-p/475556#M122304</guid>
      <dc:creator>Pankp</dc:creator>
      <dc:date>2018-07-05T10:14:14Z</dc:date>
    </item>
  </channel>
</rss>

