06-30-2011 04:50 PM
I am looking to find out how to pull a value for filesize from a file on Windows system via SAS program. I have this chunk of sas code below which I cant figure out how to use the value it generates beyond a "put" statement for the log. (This could be a bigger issue of my understanding of how to declare variables and easily reuse the values, which currently seems very counter intutive from my .net coding experience). But here is the code I am working with:
filename _in "C:\Somefile.ext";
data _null_; recfm=F end=last;
ctr is virtually gone once it puts...(caput??) I want to use this value... how do I do this? Seems like I should be able to do something like %LET filesize = ctr, but %LET apparently does not work like a declare
If someone would please let me know how to declare a variable, assign it the "ctr" value from the data block calculation and then use this value attached to my variable later in the program this will be very helpful.
infile _in lrecl=1
if last then
06-30-2011 05:38 PM
Here is one possibility using the "External File" functions.
length filename $8 infoname $32 infoval $128;
do rc = filename(filename,'file path/name here') while(rc eq 0);
do fid=fopen(filename) while(fid ne 0);
do i = 1 to foptnum(fid);
rc = filename(filename,'CLEAR');
06-30-2011 11:02 PM
If you can use pipe ability,then it will be easy.
filename fsize pipe 'dir c:\temp\'; data _null_ ; infile fsize ; input; put _infile_; run;
07-11-2011 10:27 AM
if last then
if last then
CALL SYMputX( 'CTR', ctr );
then later code will be able to use &ctr to refer to the value that was in data set variable CTR
title "&ctr items in the report" ;
07-12-2011 08:58 AM
Here is a command that we can use it for checking the file size in windows environment( through DOS prompt)
for %I in (testfile.txt) do @echo %~ZI
We can execute windows command inside SAS using 'X' Command statement or CALL SYSTEM routine.
Please try this.
07-12-2011 09:02 PM
The code you originally posted looks like it was designed to deterime the number of records in the text file, not the file size in bytes. You'll have a bit of trouble with this code under windows for a couple of reasons:
1. Your code specifies RECFM=F for the input file. This indicates that the text file has fixed length records, i.e. every line in the text file is exactly the same number of characters long. Windows doesn't produce fixed record files - windows text file are variable records length format (RECFM=V). So unless the file was created on another system (probably a mainframe) you should use RECFM=V instead of RECFM=F.
2. The default input buffer size on Windows is 256 bytes. If any individual record in the ext file is more than 256 bytes long, the record will be truncated in the input buffer. Specifying an input buffer size LARGER than the largest record has no ill effect other than consuming a few bytes more of memory. You can specify the input buffer size (Logical RECord Length) with the LRECL= option.
3. If, during a DATA step, you want to put a value into a macro variable for use in downstream program steps, you need a data step function like CALL SYMPUTX().
Try something like this:
%let File=C:\Somefile.ext; filename _in "&file" lrecl=32767; data _null_; infile _in end=last; input; ctr+1; if last then call symputx('ctr',ctr); run; filename _in clear; %PUT NOTE: &file contains &ctr records.;