<?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: IF THEN DO alternative in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335032#M75743</link>
    <description>&lt;P&gt;Awesome!&amp;nbsp; Thanks so much.&amp;nbsp; I don't think I've used the select statement in a data step before.&lt;/P&gt;</description>
    <pubDate>Wed, 22 Feb 2017 16:42:04 GMT</pubDate>
    <dc:creator>Char</dc:creator>
    <dc:date>2017-02-22T16:42:04Z</dc:date>
    <item>
      <title>IF THEN DO alternative</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335015#M75733</link>
      <description>&lt;P&gt;I'm using PC SAS v9.3.&amp;nbsp; I have some conditional logic that looks like this...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA CPA_PST(KEEP=REG LCD VARPST MEANPOST MOS);&lt;/P&gt;
&lt;P&gt;SET CPA_PST;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IF MOS = 3 THEN DO; VARPST = VAR(OF MONS1 - MONS3); MEANPOST = MEAN(OF MONS1 - MONS3); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 4 THEN DO; VARPST = VAR(OF MONS1 - MONS4); MEANPOST = MEAN(OF MONS1 - MONS4); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 5 THEN DO; VARPST = VAR(OF MONS1 - MONS5); MEANPOST = MEAN(OF MONS1 - MONS5); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 6 THEN DO; VARPST = VAR(OF MONS1 - MONS6); MEANPOST = MEAN(OF MONS1 - MONS6); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 7 THEN DO; VARPST = VAR(OF MONS1 - MONS7); MEANPOST = MEAN(OF MONS1 - MONS7); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 8 THEN DO; VARPST = VAR(OF MONS1 - MONS8); MEANPOST = MEAN(OF MONS1 - MONS8); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 9 THEN DO; VARPST = VAR(OF MONS1 - MONS9); MEANPOST = MEAN(OF MONS1 - MONS9); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 10 THEN DO; VARPST = VAR(OF MONS1 - MONS10); MEANPOST = MEAN(OF MONS1 - MONS10); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 11 THEN DO; VARPST = VAR(OF MONS1 - MONS11); MEANPOST = MEAN(OF MONS1 - MONS11); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 12 THEN DO; VARPST = VAR(OF MONS1 - MONS12); MEANPOST = MEAN(OF MONS1 - MONS12); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 13 THEN DO; VARPST = VAR(OF MONS1 - MONS13); MEANPOST = MEAN(OF MONS1 - MONS13); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 14 THEN DO; VARPST = VAR(OF MONS1 - MONS14); MEANPOST = MEAN(OF MONS1 - MONS14); END;&lt;/P&gt;
&lt;P&gt;ELSE IF MOS = 15 THEN DO; VARPST = VAR(OF MONS1 - MONS15); MEANPOST = MEAN(OF MONS1 - MONS15); END;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However I could (and probably will) have a different max number of mons to calculate going forward.&amp;nbsp; Rather than having to manually add a line each month, is there a way to use mos as my max mons value?&amp;nbsp; I'm thinking something like this...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA CPA_PST(KEEP=REG LCD VARPST MEANPOST MOS);&lt;/P&gt;
&lt;P&gt;SET CPA_PST;&lt;/P&gt;
&lt;P&gt;VARPST = VAR(OF MONS1 - 'MONS'||trim(left(mos))); MEANPOST = MEAN(OF MONS1 - 'MONS'||trim(left(mos)));&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...but SAS has no idea what I'm talking about.&amp;nbsp; I'm just at a loss as to how to do this.&amp;nbsp; Any help is appreciated!&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2017 16:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335015#M75733</guid>
      <dc:creator>Char</dc:creator>
      <dc:date>2017-02-22T16:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN DO alternative</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335019#M75735</link>
      <description>so are you saying you just needed the max of Mons instead of all the existing lines (i.i 1 to 15) ?</description>
      <pubDate>Wed, 22 Feb 2017 16:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335019#M75735</guid>
      <dc:creator>anoopmohandas7</dc:creator>
      <dc:date>2017-02-22T16:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN DO alternative</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335020#M75736</link>
      <description>&lt;P&gt;Ouch.&lt;/P&gt;
&lt;P&gt;Look at the documentation of the select statement (data step language).&lt;/P&gt;
&lt;P&gt;Your code would look like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select(mos);
  when (3) do;
    varpst = var(of mons1 - mons3);
    meanpost = mean (of mons1 - mons3);
  end;
  .........
  otherwise; /* if necessary */
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, I'd pack that into a macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro my_loop(max_mon);
data cpa (keep=reg lcd varpst meanpost mos);
set cpa_pst;
select(mos);
%do i = 3 %to &amp;amp;max_mon.;
  when (&amp;amp;i.) do;
    varpst = var(of mons1 - mons&amp;amp;i.);
    meanpost = mean (of mons1 - mons&amp;amp;i.);
  end;
%end;
  otherwise; /* if necessary */
end;
%mend;

%my_loop(15)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Feb 2017 16:27:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335020#M75736</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-02-22T16:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN DO alternative</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335024#M75739</link>
      <description>&lt;P&gt;use the COLON to select all variables that match a specified prefix:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data Want;
set Have;
array Y[*] MONS: ;
meanpst = mean(of Y[*]);
varpst = var(of Y[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Feb 2017 16:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335024#M75739</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-22T16:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN DO alternative</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335032#M75743</link>
      <description>&lt;P&gt;Awesome!&amp;nbsp; Thanks so much.&amp;nbsp; I don't think I've used the select statement in a data step before.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2017 16:42:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335032#M75743</guid>
      <dc:creator>Char</dc:creator>
      <dc:date>2017-02-22T16:42:04Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN DO alternative</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335039#M75747</link>
      <description>&lt;P&gt;Read about the SELECT statement in the article &lt;A href="http://blogs.sas.com/content/iml/2016/06/20/select-when-sas-data-step.html" target="_self"&gt;"The SELECT statement in the SAS DATA step."&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2017 17:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335039#M75747</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-22T17:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN DO alternative</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335041#M75748</link>
      <description>&lt;P&gt;Perhaps you don't need to do all of that work?&lt;/P&gt;
&lt;P&gt;It looks to me that the variable MOS has the number of values you want to include. If the rest of the variables have missing values then the &amp;nbsp;MEAN() and VAR() statement will handle that automatically. &amp;nbsp;Try running this little test and see if there are any cases in your data where MOS is &lt;STRONG&gt;NOT&lt;/STRONG&gt; the number of non-missing values in your list of data values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set CPA_PST;
  if mos ne n(of mons1-mons15) then do;
    put (mos mons1-mons15) (=);
    stop;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If not then you data step can just be this simple.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set cpa_pst ;
  if mos &amp;gt;= 3 then do;
    varpst = var(of mons1-mons15);
    meanpost=mean(of mons1-mons15);
  end;
  keep reg lcd varpst meanpost mos;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Feb 2017 17:23:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335041#M75748</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-02-22T17:23:56Z</dc:date>
    </item>
    <item>
      <title>Re: IF THEN DO alternative</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335366#M75892</link>
      <description>&lt;P&gt;Will do.&amp;nbsp; Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 17:54:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN-DO-alternative/m-p/335366#M75892</guid>
      <dc:creator>Char</dc:creator>
      <dc:date>2017-02-23T17:54:50Z</dc:date>
    </item>
  </channel>
</rss>

