<?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: I would like to do the calculation only the variable exists within a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546862#M151488</link>
    <description>&lt;P&gt;Thank you so much, that works absolutely&amp;nbsp;fine without any warnings.&lt;/P&gt;</description>
    <pubDate>Thu, 28 Mar 2019 12:03:21 GMT</pubDate>
    <dc:creator>Sami1234</dc:creator>
    <dc:date>2019-03-28T12:03:21Z</dc:date>
    <item>
      <title>I would like to do the calculation only the variable exists within a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546823#M151472</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Wondering if someone can help me, please?&lt;/P&gt;&lt;P&gt;I would like to&amp;nbsp;create another variable depending on if the variable exists&amp;nbsp;in the dataset.&lt;/P&gt;&lt;P&gt;If the variable does not exists in the dataset then no need to create the variable.&lt;/P&gt;&lt;P&gt;At the moment, it's creating a blank variable for M0 and STD0 (as they don't&amp;nbsp;exist) and throwing a warning in the log for the uninitialized&amp;nbsp;variables.&lt;/P&gt;&lt;P&gt;Also, would it be possible to create a loop or an array to do the checks and create (diff0-diff9) variables for up to&amp;nbsp;10 variables (m0-m9) depending on if the correspondence&amp;nbsp;variables exist?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data testb;&lt;/P&gt;&lt;P&gt;set testa;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;if _n_=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;then do;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;dsid=open(&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#800080"&gt;'testa'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;varexistm0 = varnum(dsid,&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#800080"&gt;'M0'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;varexistm1 = varnum(dsid,&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#800080"&gt;'M1'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;varexistm2 = varnum(dsid,&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#800080"&gt;'M2'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;varexists0 = varnum(dsid,&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#800080"&gt;'STD0'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;varexists1 = varnum(dsid,&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#800080"&gt;'STD1'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;varexists2 = varnum(dsid,&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#800080"&gt;'STD2'&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;) ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;rc=close(dsid);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;drop rc dsid;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;if varexistm0 &amp;gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and&amp;nbsp;&amp;nbsp;varexists0 &amp;gt;0 then do;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;diff0= (m10 - m0) / sqrt((std10**&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;+ std0**&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;) /&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;if varexistm1 &amp;gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and&amp;nbsp;&amp;nbsp;varexists1 &amp;gt;0 then do; then do;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;diff1= (m10 - m1) / sqrt((std10**&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;+ std1**&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;) /&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&amp;nbsp;end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;if varexistm2 &amp;gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and&amp;nbsp;&amp;nbsp;varexists2 &amp;gt;0 then do;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;diff2= (m10 - m2) / sqrt((std10**&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;+ std2**&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;) /&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;);&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;&lt;FONT face="Courier New" size="2"&gt;end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;run;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2019 08:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546823#M151472</guid>
      <dc:creator>Sami1234</dc:creator>
      <dc:date>2019-03-28T08:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the calculation only the variable exists within a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546837#M151478</link>
      <description>&lt;P&gt;Pull the metadata from dictionary.columns, and then use da data _null_ step to dynamically generate the code:&lt;/P&gt;
&lt;P&gt;(I used a simple subtraction as the mathematical operation to be performed)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ m0 m1 std0 std1;
datalines;
x 1 2 3 5
;
run;

proc sql noprint;
create table variables as
select name, substr(name,length(name)) as seq
from dictionary.columns
where libname = 'WORK' and memname = 'HAVE' and anydigit(calculated seq);
select name into :vars separated by ' ' from variables order by seq, name;
quit;

data _null_;
call execute("data want; set have;");
do i = 1 to countw("&amp;amp;vars.") by 2;
  put _all_;
  targetvar = "diff" !! substr(scan("&amp;amp;vars.",i),length(scan("&amp;amp;vars.",i)));
  call execute(targetvar !! '=' !! scan("&amp;amp;vars.",i+1) !! '-' !! scan("&amp;amp;vars.",i) !! ';');
end;
call execute("run;");
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id    m0    m1    std0    std1    diff0    diff1

x      1     2      3       5       2        3  
&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Mar 2019 09:54:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546837#M151478</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-28T09:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the calculation only the variable exists within a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546843#M151481</link>
      <description>&lt;P&gt;The variables are created even if you do not write to them in your datastep.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would split the code in two datasteps, and use the first datastep to generate code for the second:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename tempsas temp;

data _null_;
dsid=open('testa');
varexistm0 = varnum(dsid,'M0') ;
varexistm1 = varnum(dsid,'M1') ;
varexistm2 = varnum(dsid,'M2') ;
varexists0 = varnum(dsid,'STD0') ;
varexists1 = varnum(dsid,'STD1') ;
varexists2 = varnum(dsid,'STD2') ;
rc=close(dsid);
end;
 
file tempsas;
if varexistm0 &amp;gt; 0 and  varexists0 &amp;gt;0 then 
  put 'diff0= (m10 - m0) / sqrt((std10**2 + std0**2) / 2);';      
 
if varexistm1 &amp;gt; 0 and  varexists1 &amp;gt;0 then 
  put 'diff1= (m10 - m1) / sqrt((std10**2 + std1**2) / 2);';

if varexistm2 &amp;gt; 0 and  varexists2 &amp;gt;0 then 
put 'diff2= (m10 - m2) / sqrt((std10**2 + std2**2) / 2);';
run;


data testb;
  set testa;
  %include tempsas/source2;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Mar 2019 10:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546843#M151481</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-03-28T10:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the calculation only the variable exists within a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546862#M151488</link>
      <description>&lt;P&gt;Thank you so much, that works absolutely&amp;nbsp;fine without any warnings.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2019 12:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-calculation-only-the-variable-exists/m-p/546862#M151488</guid>
      <dc:creator>Sami1234</dc:creator>
      <dc:date>2019-03-28T12:03:21Z</dc:date>
    </item>
  </channel>
</rss>

