<?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: How to count number of columns with values in an array? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290353#M270010</link>
    <description>&lt;P&gt;Thank you everbody for the replies. As you mentioned above, adjusting the data before transposing was also on my mind, but I just wanted to see if there is any way to get the solution by using arrays (as I am trying to get more efficient in arrays).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the codes are really helpful I appreciate your time posting the possible solutions!&lt;/P&gt;</description>
    <pubDate>Mon, 08 Aug 2016 23:28:56 GMT</pubDate>
    <dc:creator>krm</dc:creator>
    <dc:date>2016-08-08T23:28:56Z</dc:date>
    <item>
      <title>How to count number of columns with values in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290308#M270003</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a sample array&amp;nbsp;below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input 
  owner_id $ 1-2 car1 $ 3-6 car2 $ 8-11 car3 $13-16 car4 $18-21 car5 $23-26;
datalines;
1 1231 2131 XXXX
2 0999 3232 9099 1111
3 5675 6776 5675 4564 3445
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to count each column&amp;nbsp;which has&amp;nbsp;a value as "1" (besides "XXXX") and create a column called "with_mileage". If a column has XXXX in it I want to create another column called "no_mileage", please see the sample&amp;nbsp;below:&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;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input 
  owner_id $ 1-2 car1 $ 3-6 car2 $ 8-11 car3 $13-16 car4 $18-21 car5 $23-26 with_mileage $ 27-28 no_mileage $ 30-31;
datalines;
1 1231 2131 XXXX           2 1
2 0999 3232 9099 1111      4 0
3 5675 6776 5675 4564 3445 5 0
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also another question is: in this sample the car number is fixed at 5. In the data that I have, I am transposing the data to get the array. However the number of columns may change with each update. Is it possible to write a code that automatically picks up the number of columns&amp;nbsp;rather than assigning a fixed value?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not really familiar with loops and arrays so any help would be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 20:35:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290308#M270003</guid>
      <dc:creator>krm</dc:creator>
      <dc:date>2016-08-08T20:35:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of columns with values in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290311#M270004</link>
      <description>&lt;P&gt;Do you have the ability to change your input data? Are you reading the data from a text file?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 20:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290311#M270004</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-08T20:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of columns with values in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290315#M270005</link>
      <description>&lt;P&gt;No, it's not from a text file. I am getting my data from a SAS table, I have the ability to change it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 20:51:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290315#M270005</guid>
      <dc:creator>krm</dc:creator>
      <dc:date>2016-08-08T20:51:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of columns with values in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290319#M270006</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Also another question is: in this sample the car number is fixed at 5. In the data that I have, I am transposing the data to get the array.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;

I was going to suggest this. Why not work with the data in the long format? It's easier to run a proc freq and count the data that way.</description>
      <pubDate>Mon, 08 Aug 2016 20:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290319#M270006</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-08T20:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of columns with values in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290320#M270007</link>
      <description>&lt;P&gt;Count BEFORE you transpose.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
   create table middle as 
   select owner_id, car
       , max( car='XXXXX') as no_milage
       , sum( case car in ('XXXXX',' ') 0 else 1 end) as with_milage
   from have
   group by owner_id 
   ;
quit;

proc transpose data=middle out=want  prefix=car;
  by owner_id with_milage no_milage ;
  var car ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Aug 2016 20:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290320#M270007</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-08-08T20:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of columns with values in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290321#M270008</link>
      <description>&lt;P&gt;What is the purpose of transposing? is it just for the array? if your purpose is to count it per owner_id, then there is simpler approach, such as;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *, sum(car ne 'XXXX' and not missing(car)) as with_mileage, sum(car eq 'XXXX') as no_mileage
from have
group by owner_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;'have' is pre-tranposed data.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 21:03:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290321#M270008</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-08-08T21:03:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of columns with values in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290323#M270009</link>
      <description>&lt;P&gt;Using the transposed data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array cars {*} car: ;&lt;/P&gt;
&lt;P&gt;with_mileage=0;&lt;/P&gt;
&lt;P&gt;no_mileage=0;&lt;/P&gt;
&lt;P&gt;do _n_=1 to dim(cars);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if cars{_n_}='XXXX' then no_mileage + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if cars{_n_} &amp;gt; ' ' then with_mileage + 1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The abbreviation CAR: refers to all variable names that begin with CAR.&amp;nbsp; So there better not be any surprise names for variables that begin with CAR but aren't supposed to be counted in the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 21:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290323#M270009</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-08T21:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of columns with values in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290353#M270010</link>
      <description>&lt;P&gt;Thank you everbody for the replies. As you mentioned above, adjusting the data before transposing was also on my mind, but I just wanted to see if there is any way to get the solution by using arrays (as I am trying to get more efficient in arrays).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the codes are really helpful I appreciate your time posting the possible solutions!&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 23:28:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-columns-with-values-in-an-array/m-p/290353#M270010</guid>
      <dc:creator>krm</dc:creator>
      <dc:date>2016-08-08T23:28:56Z</dc:date>
    </item>
  </channel>
</rss>

