<?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: &amp;quot;if...then do;...end;&amp;quot; statement generates missing values in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724099#M80263</link>
    <description>&lt;P&gt;How about running this code:&lt;/P&gt;
&lt;PRE&gt;data junk; 
   set OR;
   newvar = quote(effect);
run;
proc print data=junk noobs;
   var newvar;
run;&lt;/PRE&gt;
&lt;P&gt;and show us the result by pasting into a text box opened on the forum using the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One suspects one or more spelling problems possible with the values of effect in the IF statements. The sneakiest to find are often spaces in the front of the value or in the middle where proportionate fonts make it hard to see the embedded spaces. The Quote function I show will place " " around the value and if there is a leading space (or two) it should be noticeable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc Print and most of the procs like Freq will left align text and "lose" a leading space. So you may not see it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Mar 2021 23:53:26 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-03-05T23:53:26Z</dc:date>
    <item>
      <title>"if...then do;...end;" statement generates missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724070#M80262</link>
      <description>&lt;P&gt;Hello there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used "if...then do;...end;" statement to get a new dataset, however, the new created variables are all missing values. I already tried the same format codes to generate another similar dataset with no errors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my codes:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data my_OR; set OR;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="LDL_10 at gluc_10=8.80" then do group=1; gluc=88; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="LDL_10 at gluc_10=9.90" then do group=1; gluc=99; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="LDL_10 at gluc_10=12.50" then do group=1; gluc=125; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="Gluc_10 at LDL_10=9.05" then do group=2; LDL=90.5; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="Gluc_10 at LDL_10=13.51" then do group=2; LDL=135.1; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="Gluc_10 at LDL_10=18.75" then do group=2; LDL=187.5; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The log looks fine.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;NOTE: There were 6 observations read from the data set WORK.OR.&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;NOTE: The data set WORK.MY_OR has 6 observations and 8 variables.&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;NOTE: DATA statement used (Total process time):&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;real time 0.01 seconds&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;cpu time 0.03 seconds&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="aw016_0-1614982497428.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55547i0653CB93A1CB8771/image-size/medium?v=v2&amp;amp;px=400" role="button" title="aw016_0-1614982497428.png" alt="aw016_0-1614982497428.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 22:18:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724070#M80262</guid>
      <dc:creator>aw016</dc:creator>
      <dc:date>2021-03-05T22:18:31Z</dc:date>
    </item>
    <item>
      <title>Re: "if...then do;...end;" statement generates missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724099#M80263</link>
      <description>&lt;P&gt;How about running this code:&lt;/P&gt;
&lt;PRE&gt;data junk; 
   set OR;
   newvar = quote(effect);
run;
proc print data=junk noobs;
   var newvar;
run;&lt;/PRE&gt;
&lt;P&gt;and show us the result by pasting into a text box opened on the forum using the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One suspects one or more spelling problems possible with the values of effect in the IF statements. The sneakiest to find are often spaces in the front of the value or in the middle where proportionate fonts make it hard to see the embedded spaces. The Quote function I show will place " " around the value and if there is a leading space (or two) it should be noticeable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc Print and most of the procs like Freq will left align text and "lose" a leading space. So you may not see it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 23:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724099#M80263</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-05T23:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: "if...then do;...end;" statement generates missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724102#M80264</link>
      <description>&lt;P&gt;Your IF statements are not formatted correctly. To do multiple assignments in each IF condition, you need a colon after the DO.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_OR; 
set OR;


if effect="LDL_10 at gluc_10=8.80" then do; group=1; gluc=88; end;
else if effect="LDL_10 at gluc_10=9.90" then do; group=1; gluc=99; end;
else if effect="LDL_10 at gluc_10=12.50" then do; group=1; gluc=125; end;
else if effect="Gluc_10 at LDL_10=9.05" then do; group=2; LDL=90.5; end;
else if effect="Gluc_10 at LDL_10=13.51" then do; group=2; LDL=135.1; end;
else if effect="Gluc_10 at LDL_10=18.75" then do; group=2; LDL=187.5; end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It also seems like you could simplify this drastically:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if find(effect, "LDL")&amp;gt;0 then do; 
group=1; 
LDL = input(scan(effect, 2, "="), 8.)*10;
end;
else do;
 group = 2;
gluc = input(scan(effect, 2, "="), 8.)*10;
end;

/*or this way, which is likely what I would do*/
if find(effect, "LDL")&amp;gt;0 then group=1;
else group=2; 
measure = input(scan(effect, 2, "="), 8.)*10;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/354589"&gt;@aw016&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello there,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used "if...then do;...end;" statement to get a new dataset, however, the new created variables are all missing values. I already tried the same format codes to generate another similar dataset with no errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my codes:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data my_OR; set OR;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="LDL_10 at gluc_10=8.80" then do group=1; gluc=88; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="LDL_10 at gluc_10=9.90" then do group=1; gluc=99; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="LDL_10 at gluc_10=12.50" then do group=1; gluc=125; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="Gluc_10 at LDL_10=9.05" then do group=2; LDL=90.5; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="Gluc_10 at LDL_10=13.51" then do group=2; LDL=135.1; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;if effect="Gluc_10 at LDL_10=18.75" then do group=2; LDL=187.5; end;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The log looks fine.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;NOTE: There were 6 observations read from the data set WORK.OR.&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;NOTE: The data set WORK.MY_OR has 6 observations and 8 variables.&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;NOTE: DATA statement used (Total process time):&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;real time 0.01 seconds&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;cpu time 0.03 seconds&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="aw016_0-1614982497428.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55547i0653CB93A1CB8771/image-size/medium?v=v2&amp;amp;px=400" role="button" title="aw016_0-1614982497428.png" alt="aw016_0-1614982497428.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 00:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724102#M80264</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-06T00:47:22Z</dc:date>
    </item>
    <item>
      <title>Re: "if...then do;...end;" statement generates missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724114#M80266</link>
      <description>&lt;P&gt;Although&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;said you should put semi-colons after DO (and I agree for purposes of good form and clarity), that is not the reason your program fails.&amp;nbsp; I used it unchanged below and it works as expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this particular case the absence of a semi-colon works just fine, because the statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if effect="LDL_10 at gluc_10=8.80" then do group=1; gluc=88; end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is effectively&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if effect="LDL_10 at gluc_10=8.80" then do group=1 &lt;EM&gt;&lt;STRONG&gt;while (group=1);&lt;/STRONG&gt;&lt;/EM&gt; gluc=88; end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit: (added the "while (group=1)" component above).&amp;nbsp; The prior "do group=1 to 1" ended up incrementing group to 2.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably thought of GROUP as just one of the variables to be assigned a value inside a DO group (I know I did at first), but SAS thinks GROUP is a do loop index.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I said the problem is not in the code - it has to be in the data.&amp;nbsp; Your code seems to work unchanged here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data OR;
 length effect $30;
 effect="LDL_10 at gluc_10=8.80" ; output;
 effect="LDL_10 at gluc_10=9.90" ; output;
 effect="LDL_10 at gluc_10=12.50"; output;
 effect="Gluc_10 at LDL_10=9.05" ; output;
 effect="Gluc_10 at LDL_10=13.51"; output;
 effect="Gluc_10 at LDL_10=18.75"; output;
run;

data my_OR; set OR;
if effect="LDL_10 at gluc_10=8.80" then do group=1; gluc=88; end;
if effect="LDL_10 at gluc_10=9.90" then do group=1; gluc=99; end;
if effect="LDL_10 at gluc_10=12.50" then do group=1; gluc=125; end;
if effect="Gluc_10 at LDL_10=9.05" then do group=2; LDL=90.5; end;
if effect="Gluc_10 at LDL_10=13.51" then do group=2; LDL=135.1; end;
if effect="Gluc_10 at LDL_10=18.75" then do group=2; LDL=187.5; end;
put (_all_) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;which produces populated values for group, gluc and LDL in the log.&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 08:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724114#M80266</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-06T08:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: "if...then do;...end;" statement generates missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724154#M80270</link>
      <description>&lt;P&gt;Right:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do group=1; ... end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is an example of this syntax:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do group=1,5,10; ... end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but with only one item in the list of values.&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 16:08:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/quot-if-then-do-end-quot-statement-generates-missing-values/m-p/724154#M80270</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-06T16:08:35Z</dc:date>
    </item>
  </channel>
</rss>

