<?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 How to Count numeric variables in an array? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781450#M31790</link>
    <description>&lt;P&gt;Greetings Everyone,&lt;/P&gt;&lt;P&gt;I want to create a new variable that is equal to number of 6 numeric variables using an array. I need to count non missing values in these variables..&lt;/P&gt;&lt;P&gt;data New;&lt;BR /&gt;set old;&lt;BR /&gt;Retain CT 0;&lt;BR /&gt;Array A[3] a b c ;&lt;BR /&gt;Do i = 1 to 3;&lt;BR /&gt;if A[i] = .&amp;nbsp; then CT= . ;&lt;BR /&gt;else If A[i] NE . then CT= a+b+c;&lt;BR /&gt;end;&lt;BR /&gt;Drop i;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code above is working but it gave incorrect large number for the data I am testing..&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
    <pubDate>Sat, 20 Nov 2021 12:20:57 GMT</pubDate>
    <dc:creator>Emet</dc:creator>
    <dc:date>2021-11-20T12:20:57Z</dc:date>
    <item>
      <title>How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781450#M31790</link>
      <description>&lt;P&gt;Greetings Everyone,&lt;/P&gt;&lt;P&gt;I want to create a new variable that is equal to number of 6 numeric variables using an array. I need to count non missing values in these variables..&lt;/P&gt;&lt;P&gt;data New;&lt;BR /&gt;set old;&lt;BR /&gt;Retain CT 0;&lt;BR /&gt;Array A[3] a b c ;&lt;BR /&gt;Do i = 1 to 3;&lt;BR /&gt;if A[i] = .&amp;nbsp; then CT= . ;&lt;BR /&gt;else If A[i] NE . then CT= a+b+c;&lt;BR /&gt;end;&lt;BR /&gt;Drop i;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code above is working but it gave incorrect large number for the data I am testing..&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 12:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781450#M31790</guid>
      <dc:creator>Emet</dc:creator>
      <dc:date>2021-11-20T12:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781452#M31791</link>
      <description>&lt;P&gt;UNTESTED CODE — counting number of non-missing values in variables A B C&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
    set old;
    count_non_missing=n(a,b,c);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 20 Nov 2021 12:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781452#M31791</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-20T12:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781453#M31792</link>
      <description>&lt;P&gt;So, I shouldn't use an array..&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 12:28:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781453#M31792</guid>
      <dc:creator>Emet</dc:creator>
      <dc:date>2021-11-20T12:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781457#M31794</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Method provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;works fine. See below.&lt;/P&gt;
&lt;P&gt;I also added another way of doing the same thing with an array (so you have two times "the same" in one data step).&lt;/P&gt;
&lt;P&gt;Choose the method of your preference.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=i);
 set sashelp.class;
 if      _N_=1  then height=.;
 else if _N_=11 then age=.;
 array numvars{3} age height weight;
 numvars_not_missing=0;
 do i=1 to dim(numvars);
  if numvars(i) ^= . then numvars_not_missing=numvars_not_missing+1;
 end;
 count_non_missing=n(age,height,weight);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 13:36:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781457#M31794</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-11-20T13:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781459#M31795</link>
      <description>&lt;P&gt;You can also use kind of an implicit array _NUMERIC_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set sashelp.class;
 if      _N_=1  then height=.;
 else if _N_=11 then age=.;
 count_non_missing=n(of _NUMERIC_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 13:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781459#M31795</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-11-20T13:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781461#M31796</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You can also use kind of an implicit array _NUMERIC_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set sashelp.class;
 if      _N_=1  then height=.;
 else if _N_=11 then age=.;
 count_non_missing=n(of _NUMERIC_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This only works if the question is how to count the number of non-missing values from ALL numeric variables in the data set. This was never specified as a criterion in this problem. Using _NUMERIC_ would give the wrong answer in many cases. &lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 14:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781461#M31796</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-20T14:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781462#M31797</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This only works if the question is how to count the number of non-missing values from ALL numeric variables in the data set. This was never specified as a criterion in this problem. Using _NUMERIC_ would give the wrong answer in many cases.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Absolutely true.&lt;/P&gt;
&lt;P&gt;I have read the original post again, and I was perhaps a little too hasty in my response. But anyway, it is apparently what&amp;nbsp;the questioner (&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/407246"&gt;@Emet&lt;/a&gt;&amp;nbsp;)&amp;nbsp; wanted, because she has accepted the answer as a solution.&lt;BR /&gt;I will add a "disclaimer" next time : that _NUMERIC_ is looking at ALL numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 14:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781462#M31797</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-11-20T14:19:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781464#M31798</link>
      <description>&lt;P&gt;I think what you are trying to say is that you don't have to define an array and can instead just use the N() function with the list of variables directly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you can use the OF keyword to allow the use of variable lists in a function call.&lt;/P&gt;
&lt;P&gt;Such as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;n(of v1-v10)
n(of a b c)
n(of array_name[*])&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 15:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781464#M31798</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-20T15:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781465#M31799</link>
      <description>&lt;P&gt;Thanks for the responses across the thread.. Your comments are very helpful as I am a new SAS user.&lt;/P&gt;&lt;P&gt;Actually, I am interested to use arrays and I wanted to know if functions like N, Nmiss, Sum would simply work.&lt;/P&gt;&lt;P&gt;e.g. B= sum(A [i] )&lt;/P&gt;&lt;P&gt;it didn't work this way but I will try adding of as Tom suggested.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 14:57:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781465#M31799</guid>
      <dc:creator>Emet</dc:creator>
      <dc:date>2021-11-20T14:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781466#M31800</link>
      <description>&lt;P&gt;The code cannot work as posted since you cannot define an array named A that contains a variable named A.&amp;nbsp; The names used for arrays have to be distinct from the names used for variables in the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need the RETAIN, unless you want to make a grand total over all of the observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't want total of A,B and C if the goal is just to count non-missing values.&amp;nbsp; Plus if did want to actually sum the values of A, B and C do not use the addition operator.&amp;nbsp; If you use the addition operator then if any of them have a missing value the result is a missing value.&amp;nbsp; So total those three variables you can use the SUM() function and missing values will be ignored.&amp;nbsp; total=sum(a,b,c)&amp;nbsp; or perhaps total=sum(0,a,b,c).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to test for missing values it is more precise to use MISSING() function instead of comparing to the missing value.&amp;nbsp; That is because SAS has 27 other special missing values that the MISSING() function will detect but your equality test will not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To find the number of non missing values from a set of values you can use the N() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
  set old;
  CT=n(a,b,c);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To use a list of variables in a function call you can use the OF keyword.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  CT=n(of a b c);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 20 Nov 2021 15:02:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781466#M31800</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-20T15:02:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781467#M31801</link>
      <description>&lt;P&gt;Super helpful! Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 20 Nov 2021 15:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781467#M31801</guid>
      <dc:creator>Emet</dc:creator>
      <dc:date>2021-11-20T15:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count numeric variables in an array?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781468#M31802</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/407246"&gt;@Emet&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the responses across the thread.. Your comments are very helpful as I am a new SAS user.&lt;/P&gt;
&lt;P&gt;Actually, I am interested to use arrays and I wanted to know if functions like N, Nmiss, Sum would simply work.&lt;/P&gt;
&lt;P&gt;e.g. B= sum(A [i] )&lt;/P&gt;
&lt;P&gt;it didn't work this way but I will try adding of as Tom suggested.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you call the SUM() function with only one argument then you should just be using an assignment statement.&amp;nbsp; So your statement is the same as :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;b=a[i];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to sum all of the variables in an array you can use * as the index to get a variable list that consists of all of the variables in the array.&amp;nbsp; To use a variable list in a function call you need the OF keyword.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sum_a = sum(of a[*]);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 20 Nov 2021 15:07:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Count-numeric-variables-in-an-array/m-p/781468#M31802</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-20T15:07:45Z</dc:date>
    </item>
  </channel>
</rss>

