<?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: array to count and set missing values in one step vs. multiple steps in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877117#M346508</link>
    <description>&lt;P&gt;Because the second IF statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ct_pre &amp;gt;= 2 then vars{j} = .;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is testing the value of CT_PRE at a different point in its development.&lt;/P&gt;
&lt;P&gt;In the first step you wait until you have counted ALL of the elements in the first array.&amp;nbsp; In the second one you are testing the value of CT_PRE before it has finished being calculated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So get the count before the DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = i); 
  set have; 
  array vars {7} var1-var7; 
  ct_pre = nmiss( of vars{*});
  if ct_pre &amp;gt;= 2 then do i = 1 to 7; 
    vars{i} = .; 
  end;
  ct_post = nmiss( of vars{*});
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 23 May 2023 16:55:20 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-05-23T16:55:20Z</dc:date>
    <item>
      <title>array to count and set missing values in one step vs. multiple steps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877116#M346507</link>
      <description>&lt;P&gt;I am trying to count the number of missing values on a set of vars.&amp;nbsp; Then, if there are two or more vars with missing values, I want to set all the vars to have missing values.&amp;nbsp; The following works:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = i j k); set have; 
ct_pre = 0; 
ct_post = 0; 
array vars (7) var1-var7; 
do i = 1 to 7; 
	if vars{i} = . then ct_pre +1; 
end; 
do j = 1 to 7; 
	if ct_pre &amp;gt;= 2 then vars{j} = .; 
end; 
do k = 1 to 7; 
	if vars{k} = . then ct_post +1; 
end; 
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thus I end up with ct_post having only values 0, 1, or 7, as desired.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I can't wrap my head around why this other, seemingly simpler approach doesn't work.&amp;nbsp; Some variables do get set to missing in some cases, but other times they do not get set to missing.&amp;nbsp; Thus, as in ct_pre, ct_post ends up with values 0-7, although a handful of the cases change in value.&amp;nbsp; There does not appear to be a method to the madness, as in I don't see any strange patterns with specific vars.&amp;nbsp; What does it have to do--I assume--with operating within only a single do/end space?&amp;nbsp; I feel like I'm not understanding something fundamental, so if anyone can explain, I would appreciate it!&amp;nbsp; Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = i); set have; 
ct_pre = 0; 
ct_post = 0; 
array vars (7) var1-var7; 
do i = 1 to 7; 
	if vars{i} = . then ct_pre +1; 
	if ct_pre &amp;gt;= 2 then vars{i} = .; 
	if vars{i} = . then ct_post +1; 
end; 
run; 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 May 2023 16:40:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877116#M346507</guid>
      <dc:creator>awesome_opossum</dc:creator>
      <dc:date>2023-05-23T16:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: array to count and set missing values in one step vs. multiple steps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877117#M346508</link>
      <description>&lt;P&gt;Because the second IF statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ct_pre &amp;gt;= 2 then vars{j} = .;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is testing the value of CT_PRE at a different point in its development.&lt;/P&gt;
&lt;P&gt;In the first step you wait until you have counted ALL of the elements in the first array.&amp;nbsp; In the second one you are testing the value of CT_PRE before it has finished being calculated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So get the count before the DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = i); 
  set have; 
  array vars {7} var1-var7; 
  ct_pre = nmiss( of vars{*});
  if ct_pre &amp;gt;= 2 then do i = 1 to 7; 
    vars{i} = .; 
  end;
  ct_post = nmiss( of vars{*});
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 May 2023 16:55:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877117#M346508</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-23T16:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: array to count and set missing values in one step vs. multiple steps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877121#M346510</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/434421"&gt;@awesome_opossum&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to count the number of missing values on a set of vars.&amp;nbsp; Then, if there are two or more vars with missing values, I want to set all the vars to have missing values.&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;How about this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    n_missing=nmiss(of var1-var7);
    if n_missing&amp;gt;=2 then call missing(of var1-var7);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 May 2023 16:55:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877121#M346510</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-23T16:55:24Z</dc:date>
    </item>
    <item>
      <title>Re: array to count and set missing values in one step vs. multiple steps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877122#M346511</link>
      <description>&lt;P&gt;This can be simplified as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = i j k); 

set have; 
ct_pre = 0; 
ct_post = 0; 
array vars (7) var1-var7; 

*number of missing values in the array;
ct_pre = nmiss(of vars(*));

*set all to missing if 2 or more are missing;
if ct_pre &amp;gt;= 2 then call missing(of vars(*));

*number of missing values after setting values to missing;
ct_post = nmiss(of vars(*));

run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 May 2023 16:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/array-to-count-and-set-missing-values-in-one-step-vs-multiple/m-p/877122#M346511</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-05-23T16:57:52Z</dc:date>
    </item>
  </channel>
</rss>

