<?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: Any way to get number of records on CSV file without reading in? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878373#M347029</link>
    <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37814"&gt;@Walternate&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your SAS setup allows accessing the OS command, then you can use this macro on either Windows/Linux&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro util_getFileLineCount(p_textFile=, p_rtrnMacVarName=, p_funkyString=\n);
	%LOCAL l_os l_firstObs;

	%let l_os = %substr(&amp;amp;sysscp,1,3);
	%let l_firstObs=1;

	%if (&amp;amp;l_os EQ LIN) %then
	%do;
		FILENAME filesize pipe "wc -l &amp;amp;p_textFile";
	%end;
	%else /*(&amp;amp;l_os EQ WIN)*/
	%do;
		FILENAME filesize pipe "find /V /C ""&amp;amp;p_funkyString"" &amp;amp;p_textFile";
		%let l_firstObs=2;
	%end;

	DATA _NULL_;
		INFILE filesize FIRSTOBS=&amp;amp;l_firstObs END=eof;
		INPUT;
	%if (&amp;amp;l_os EQ LIN) %then
	%do;
		CALL SYMPUTX("&amp;amp;p_rtrnMacVarName",SCAN(_INFILE_,1,' '));
	%end;
	%else
	%do;
		CALL SYMPUTX("&amp;amp;p_rtrnMacVarName",SCAN(_INFILE_,-1,':'));
	%end;
	RUN;
%mend util_getFileLineCount;

%global g_lineCount;
&lt;STRONG&gt;%util_getFileLineCount&lt;/STRONG&gt;(p_textFile=%str(&amp;lt;path/filename.type&amp;gt;), p_rtrnMacVarName=g_lineCount, p_funkyString=\n);​/* Change \n to a different value that shouldn't be in the file */
%put &amp;amp;=g_lineCount;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: You can save the util_getFileLineCount macro into it's own file util_getfilelinecount.sas for re-use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;</description>
    <pubDate>Wed, 31 May 2023 09:14:51 GMT</pubDate>
    <dc:creator>AhmedAl_Attar</dc:creator>
    <dc:date>2023-05-31T09:14:51Z</dc:date>
    <item>
      <title>Any way to get number of records on CSV file without reading in?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878256#M346981</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I know how to check the number of records on a CSV file once it has been read in to SAS, but am wondering if there is a shortcut where I could get at the # of records on the file without having to read it in first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;BR /&gt;Jenna&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 16:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878256#M346981</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2023-05-30T16:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: Any way to get number of records on CSV file without reading in?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878332#M347007</link>
      <description>&lt;P&gt;Do you know how many rows of header information may be involved?&lt;/P&gt;
&lt;P&gt;Does the end of the CSV file have "empty" rows&amp;nbsp; consisting of nothing but commas ( a frequent behavior when converting spreadsheets to CSV)?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Answers to these go to the question of "valid" observations versus lines of text in the file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you tell us what you will do differently when you have that information?&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 22:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878332#M347007</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-30T22:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: Any way to get number of records on CSV file without reading in?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878336#M347010</link>
      <description>&lt;P&gt;SAS treats csv files essentially as a stream of data, with each obs separated by a CR or CRLF, and typically terminated by an EOF (end of file indicator).&amp;nbsp; &amp;nbsp;But unlike a SAS dataset, a CSV file does not require inclusion of such metadata as the number of obs (rows).&amp;nbsp; So there is no way for you to know with certainty how many records are in the CSV file without processing it.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There can be exceptions of course.&amp;nbsp; If you know that all records have the same length - and you know (or can determine) what that length is, you can tell SAS to ask the operating system for the size of the csv file in bytes, then divide by the fixed length, in bytes, of each record to get the record count.&amp;nbsp; This is an increasingly unlikely scenario.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, if you can even make a good guess at the average record length, you can generate an equally good guess at the number of records using the CSV file size.&amp;nbsp; As an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let csv_filename=c:\temp\export.csv;
%let expected_record_length=40;

data info;
   drop rc fid close;
   rc=filename('abc', "&amp;amp;csv_filename");

   fid=fopen('abc');
   filesize=input(finfo(fid,"File Size (bytes)"),best32.);
   close=fclose(fid);   
   expected_nrecs=filesize/&amp;amp;expected_record_length;
   put filesize= expected_nrecs=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The argument "File Size (bytes)" is apparently valid in both Windows and Unix - I successfully tested it in Windows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;notes, you can improve the estimate of record counts it you know how many header records are in the CSV file.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2023 23:42:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878336#M347010</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-05-30T23:42:57Z</dc:date>
    </item>
    <item>
      <title>Re: Any way to get number of records on CSV file without reading in?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878343#M347016</link>
      <description>&lt;P&gt;No.&lt;/P&gt;
&lt;P&gt;A CSV file is text file with variable length lines.&amp;nbsp; The only way to know how many lines are in the file is to read the whole file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you don't have to actually do anything with the lines, other than count them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if eof then call symputx('num_lines', _n_-1);
  infile 'myfile.csv' end=eof;
  input;
run;
%put&amp;nbsp;Number&amp;nbsp;of&amp;nbsp;lines&amp;nbsp;in&amp;nbsp;myfile.csv&amp;nbsp;is&amp;nbsp;&amp;amp;num_lines..;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you don't have to use SAS.&amp;nbsp; For example if you are running on Unix just use the wc command with the -l option to count the number of lines in the file.&amp;nbsp; If the file has a header row then the number of lines of actual data is one less than the number of lines.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 02:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878343#M347016</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-31T02:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: Any way to get number of records on CSV file without reading in?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878356#M347021</link>
      <description>&lt;P&gt;On a UNIX system, use the external command &lt;FONT face="courier new,courier"&gt;wc -l&lt;/FONT&gt;. This is the fastest way to do it.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 06:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878356#M347021</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-31T06:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: Any way to get number of records on CSV file without reading in?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878373#M347029</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37814"&gt;@Walternate&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your SAS setup allows accessing the OS command, then you can use this macro on either Windows/Linux&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro util_getFileLineCount(p_textFile=, p_rtrnMacVarName=, p_funkyString=\n);
	%LOCAL l_os l_firstObs;

	%let l_os = %substr(&amp;amp;sysscp,1,3);
	%let l_firstObs=1;

	%if (&amp;amp;l_os EQ LIN) %then
	%do;
		FILENAME filesize pipe "wc -l &amp;amp;p_textFile";
	%end;
	%else /*(&amp;amp;l_os EQ WIN)*/
	%do;
		FILENAME filesize pipe "find /V /C ""&amp;amp;p_funkyString"" &amp;amp;p_textFile";
		%let l_firstObs=2;
	%end;

	DATA _NULL_;
		INFILE filesize FIRSTOBS=&amp;amp;l_firstObs END=eof;
		INPUT;
	%if (&amp;amp;l_os EQ LIN) %then
	%do;
		CALL SYMPUTX("&amp;amp;p_rtrnMacVarName",SCAN(_INFILE_,1,' '));
	%end;
	%else
	%do;
		CALL SYMPUTX("&amp;amp;p_rtrnMacVarName",SCAN(_INFILE_,-1,':'));
	%end;
	RUN;
%mend util_getFileLineCount;

%global g_lineCount;
&lt;STRONG&gt;%util_getFileLineCount&lt;/STRONG&gt;(p_textFile=%str(&amp;lt;path/filename.type&amp;gt;), p_rtrnMacVarName=g_lineCount, p_funkyString=\n);​/* Change \n to a different value that shouldn't be in the file */
%put &amp;amp;=g_lineCount;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: You can save the util_getFileLineCount macro into it's own file util_getfilelinecount.sas for re-use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 09:14:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-way-to-get-number-of-records-on-CSV-file-without-reading-in/m-p/878373#M347029</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-05-31T09:14:51Z</dc:date>
    </item>
  </channel>
</rss>

