<?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 Determine if a variables exists and conditionally execute additional steps in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Determine-if-a-variables-exists-and-conditionally-execute/m-p/519624#M140775</link>
    <description>&lt;P&gt;Hi I'm trying to run some basic statistics on variables that are numbered in sequence, however, not all the numbers exist in the sequence. The basic statistic tests exist in a separate macro (stddiff) that I'm calling from my do-looped macro and I only want it to call my statistics macro if the variable actually exists. I have variables from var001 - var899, however for example, var018 does not exist. This is currently what I have for my code:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro std_diff_loop (start,end); 

%do i=&amp;amp;start %to &amp;amp;end;

 
   %if %length(&amp;amp;start) eq 1 %then %do;
   %let i=%sysfunc(putN(&amp;amp;i,z3.));
   %end;

 
   %if %length(&amp;amp;start) eq 2 %then %do;
   %let i=%sysfunc(putN(&amp;amp;i,z2.));
   %end;

 

      %stddiff( inds = indicators, 
                groupvar = in_group, 
                numvars = , 
                charvars = var_n&amp;amp;i, 
                wtvar =,
               stdfmt = 8.4,
              outds = stddiff_result_var_&amp;amp;i ); 


%end;
%mend;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run it this way, I get errors for the variables that don't exist since it can't run statistics on variables that don't exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally what I'd like is for the macro to check my data set (indicators) before it calls the macro stddiff and only call the macro if the variable exists in the data set. Thanks!&lt;/P&gt;</description>
    <pubDate>Sat, 08 Dec 2018 02:18:38 GMT</pubDate>
    <dc:creator>endofline</dc:creator>
    <dc:date>2018-12-08T02:18:38Z</dc:date>
    <item>
      <title>Determine if a variables exists and conditionally execute additional steps in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-if-a-variables-exists-and-conditionally-execute/m-p/519624#M140775</link>
      <description>&lt;P&gt;Hi I'm trying to run some basic statistics on variables that are numbered in sequence, however, not all the numbers exist in the sequence. The basic statistic tests exist in a separate macro (stddiff) that I'm calling from my do-looped macro and I only want it to call my statistics macro if the variable actually exists. I have variables from var001 - var899, however for example, var018 does not exist. This is currently what I have for my code:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro std_diff_loop (start,end); 

%do i=&amp;amp;start %to &amp;amp;end;

 
   %if %length(&amp;amp;start) eq 1 %then %do;
   %let i=%sysfunc(putN(&amp;amp;i,z3.));
   %end;

 
   %if %length(&amp;amp;start) eq 2 %then %do;
   %let i=%sysfunc(putN(&amp;amp;i,z2.));
   %end;

 

      %stddiff( inds = indicators, 
                groupvar = in_group, 
                numvars = , 
                charvars = var_n&amp;amp;i, 
                wtvar =,
               stdfmt = 8.4,
              outds = stddiff_result_var_&amp;amp;i ); 


%end;
%mend;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run it this way, I get errors for the variables that don't exist since it can't run statistics on variables that don't exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally what I'd like is for the macro to check my data set (indicators) before it calls the macro stddiff and only call the macro if the variable exists in the data set. Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 08 Dec 2018 02:18:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-if-a-variables-exists-and-conditionally-execute/m-p/519624#M140775</guid>
      <dc:creator>endofline</dc:creator>
      <dc:date>2018-12-08T02:18:38Z</dc:date>
    </item>
    <item>
      <title>Re: Determine if a variables exists and conditionally execute additional steps in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-if-a-variables-exists-and-conditionally-execute/m-p/519631#M140781</link>
      <description>&lt;P&gt;There are many such macros available.&amp;nbsp; Here is link to one:&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/varexist.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/varexist.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is your macro updated to use it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro std_diff_loop (start,end);
%local i ;
%do i=&amp;amp;start %to &amp;amp;end;
  %let i=%sysfunc(putn(&amp;amp;i,z%length(&amp;amp;end).));
  %if %varexist(indicators,var_n&amp;amp;i) %then %do;
    %stddiff
    (inds = indicators
    ,groupvar = in_group
    ,numvars =
    ,charvars = var_n&amp;amp;i
    ,wtvar =
    ,stdfmt = 8.4
    ,outds = stddiff_result_var_&amp;amp;i
    )
  %end;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do use that version of the VAREXIST macro you can improve your macro's testing to make sure that it only tries to compute Std Dev on numeric variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  %if N=%varexist(indicators,var_n&amp;amp;i,type) %then %do;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Dec 2018 04:48:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-if-a-variables-exists-and-conditionally-execute/m-p/519631#M140781</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-08T04:48:43Z</dc:date>
    </item>
    <item>
      <title>Re: Determine if a variables exists and conditionally execute additional steps in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-if-a-variables-exists-and-conditionally-execute/m-p/520037#M140963</link>
      <description>&lt;P&gt;Depending on you data structure you may be doing more work than needed. For instance, are you doing the same statistics on all the variables? Are they next to each other in the data set (column order)? Perhaps you can use the var1 - - varXXX shorthand to list adjacent variables such as below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   set sashelp.class;
   rename height=var1;
   rename weight=var99;
   drop name sex age;
run;


proc means data=example;
   var var1--var99;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if you variables have a unique base name then perhaps just the colon shorthand:&amp;nbsp; var:&amp;nbsp; would use all variables with the common name stem VAR.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without seeing the actual code for you STD_DIFF macro it is difficult to see if other suggestions might be valid.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 16:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-if-a-variables-exists-and-conditionally-execute/m-p/520037#M140963</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-12-10T16:32:08Z</dc:date>
    </item>
  </channel>
</rss>

