<?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: counting number of columns in csv file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918777#M361920</link>
    <description>&lt;P&gt;I tried it again and it worked (not sure what happened the first time I tried it), Thank you!&lt;/P&gt;</description>
    <pubDate>Mon, 04 Mar 2024 11:00:04 GMT</pubDate>
    <dc:creator>Taliah</dc:creator>
    <dc:date>2024-03-04T11:00:04Z</dc:date>
    <item>
      <title>counting number of columns in csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918767#M361914</link>
      <description>&lt;P&gt;Hello, In order to detect problems with csv input files I count the number of columns in them (and compare them with the expected number), using the code:&lt;/P&gt;
&lt;P&gt;data _null;&lt;/P&gt;
&lt;P&gt;infile "&amp;amp;dir.test.csv";&lt;/P&gt;
&lt;P&gt;input; call symputx("ncol_file1", countw(_infile_,","));&lt;/P&gt;
&lt;P&gt;stop; run;&lt;/P&gt;
&lt;P&gt;This works fine when all columns have data in row 1 (the data set is without column names). When a column does not have data in row 1 the resulting number of columns is without that column. Is there a way to count a column even when the first row (or several first rows) are without data?&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 09:03:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918767#M361914</guid>
      <dc:creator>Taliah</dc:creator>
      <dc:date>2024-03-04T09:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: counting number of columns in csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918769#M361915</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/256609"&gt;@Taliah&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p18xi2516ihygyn1qg1b1nby326k.htm#n0b0ssfrm4vvf5n1ck7p4z5gqgnk" target="_blank" rel="noopener"&gt;"m" modifier&lt;/A&gt; of the COUNTW function to include blank or zero-length words in the count:&lt;/P&gt;
&lt;PRE&gt;countw(_infile_, ",", &lt;FONT color="#00DF00"&gt;&lt;STRONG&gt;"m"&lt;/STRONG&gt;&lt;/FONT&gt;)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;To avoid a result of 1 in case of a blank line (no delimiters) you could subtract 1 in that case:&lt;/P&gt;
&lt;PRE&gt;call symputx("ncol_file1", countw(_infile_, ",", &lt;STRONG&gt;&lt;FONT color="#00DF00"&gt;"m"&lt;/FONT&gt;&lt;/STRONG&gt;)&lt;STRONG&gt;&lt;FONT color="#00DF00"&gt;-(_infile_=" ")&lt;/FONT&gt;&lt;/STRONG&gt;);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: If any of the values may contain the delimiter (comma) in quotation marks (as in &lt;FONT face="courier new,courier"&gt;"Cary, NC"&lt;/FONT&gt;), include the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p18xi2516ihygyn1qg1b1nby326k.htm#n1pl9ce4kp7nyvn1jbuk77cav8n8" target="_blank" rel="noopener"&gt;"q" modifier&lt;/A&gt; as well:&lt;/P&gt;
&lt;PRE&gt;countw(_infile_, ",", &lt;FONT color="#00DF00"&gt;&lt;STRONG&gt;"mq"&lt;/STRONG&gt;&lt;/FONT&gt;)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 10:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918769#M361915</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-03-04T10:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: counting number of columns in csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918771#M361916</link>
      <description>&lt;P&gt;Would be interesting to see the code following the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 09:58:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918771#M361916</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2024-03-04T09:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: counting number of columns in csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918772#M361917</link>
      <description>&lt;P&gt;Thank you! the m modifier works great.&lt;/P&gt;
&lt;P&gt;You wrote "&lt;SPAN&gt;&amp;nbsp;To avoid a result of 1 in case of a blank line (no delimiters)" -&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;you mean if row 1 has no modofiers, and no data?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;When I add the addition you gave and the file has modifiers and data in row 1,&amp;nbsp; it subtracts 1 from the count (as you wrote), so I can't add that to the code (I am trying to understand what you are suggesting), Thank you!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 10:19:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918772#M361917</guid>
      <dc:creator>Taliah</dc:creator>
      <dc:date>2024-03-04T10:19:59Z</dc:date>
    </item>
    <item>
      <title>Re: counting number of columns in csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918773#M361918</link>
      <description>&lt;P&gt;The next step is comparing the number of columns counted with teh expected number, and if they are different the code sends a warning email and stops running (abort cancel)&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 10:21:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918773#M361918</guid>
      <dc:creator>Taliah</dc:creator>
      <dc:date>2024-03-04T10:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: counting number of columns in csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918774#M361919</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/256609"&gt;@Taliah&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You wrote "&lt;SPAN&gt;&amp;nbsp;To avoid a result of 1 in case of a blank line (no delimiters)" -&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;you mean if row 1 has no modofiers, and no data?&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I mean no delimiters and no data except blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;SPAN&gt;When I add the addition you gave and the file has modifiers and data in row 1,&amp;nbsp; it subtracts 1 from the count (as you wrote), ...&lt;/SPAN&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No, it doesn't. It subtracts 1 "&lt;SPAN&gt;&lt;EM&gt;in that case,&lt;/EM&gt;" i.e., in case of a blank line. The Boolean value&amp;nbsp;&lt;FONT face="courier new,courier"&gt;(_infile_=" ")&lt;/FONT&gt;, which is subtracted, equals 1 only if the equality&amp;nbsp;&lt;FONT face="courier new,courier"&gt;_infile_=" "&lt;/FONT&gt; is true&amp;nbsp; (this is the case of a blank line, regardless of the number of blanks it contains, zero or more) and 0 otherwise. That is, in almost all cases nothing is subtracted.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 10:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918774#M361919</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-03-04T10:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: counting number of columns in csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918777#M361920</link>
      <description>&lt;P&gt;I tried it again and it worked (not sure what happened the first time I tried it), Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 11:00:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918777#M361920</guid>
      <dc:creator>Taliah</dc:creator>
      <dc:date>2024-03-04T11:00:04Z</dc:date>
    </item>
    <item>
      <title>Re: counting number of columns in csv file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918854#M361938</link>
      <description>&lt;P&gt;That code is just trying to find the number of fields on the FIRST line of the file.&amp;nbsp; That could be done more simply with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null;
  call symputx('ncol',ncol);
  infile "&amp;amp;dir.test.csv" obs=1;
  ncol + countw(_infile_,',','mq');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will return 0 for a file with no lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought you wanted to check if the file consistently had the same number of fields on every line.&amp;nbsp; To do that you need to read the whole file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null;
  if eof then do;
    call symputx('nobs',_n_-1);
    call symputx('ncol',ncol);
    call symputx('nbad',nbad);
  end;
  infile "&amp;amp;dir.test.csv" end=eof;
  if _n_=1 then ncol = countw(_infile_,',','mq');
  else nbad + (ncol ne countw(_infile_,',','mq'));
  retain ncol nbad 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 17:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-number-of-columns-in-csv-file/m-p/918854#M361938</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-04T17:27:47Z</dc:date>
    </item>
  </channel>
</rss>

