SAS to count excel observations

不问归期 提交于 2019-12-24 11:28:01

问题


I have to carry out a reconciliation of accounts for a number of excel files approximately 500 excel files.

I don’t need to match accounts numbers just the volume by file.

Example file 1: \\directory\Loaded\Jan2014\excel1

Example file 2: \\directory\Loaded\Feb2014\excel2

Example file 3: \\directory\Loaded\Feb2014\excel3

(account number is always populated in column B with rows 1 to 5 as the titles)

Required output using above example:

Main Folder (File) | Sub Folder (Jan2014) | File Name (excel1) | count of account numbers

Is this possible using SAS?

So if this is not enough information, i have searched the net and found ways using batch files to bring back a list of files but nothing that counts the observations.


回答1:


The SAS solution is something like this. You could make this a bit more efficient if you did all of the libnames and then set all of the datasets, but this code is a bit easier, and for 500 I think it's reasonable. Unfortunately excel libnames don't seem to do rowcounts for you, so you can't just use dictionary.tables to do this.

If the sheet names vary, you will need to modify this to take that into account, either by setting up a macro variable that holds what the sheet name should be if it's linked to the filename in some way, or by having the macro do a query to dictionary.tables to see what tables are present in the libname.

%let basedir=c:\temp;  *whatever the base directory is that all of your excel files are upstream from;
filename dirl pipe "dir /b/s &basedir.\*.xlsx";

data libnames;
infile dirl lrecl=1024 pad;
input
@1 filename $1024.;
run;

%macro get_rowcount(file=);
  libname _temp excel "&file.";
  data _rowcount;
  set _temp."Sheet1$"n end=eof;
  length file_name $1024;
  retain file_name "&file.";
  if eof then do;
    rowcount=_n_;
    output;
  end;
  keep rowcount file_name;
  run;

  proc append base=rowcounts data=_rowcount force;
  run;
%mend get_rowcount;

proc sql;
select cats('%get_rowcount(file=',filename,')') into :sheetlist separated by ' '
  from libnames;
quit;
&sheetlist.;



回答2:


I'd use Powershell for this, unless you really need to do it in SAS. To get the result to SAS, if needed, you can save data from powershell to excel file and import it to SAS.

Have a look at e.g. Get the Number of Rows in Column of a worksheet as a starter.



来源:https://stackoverflow.com/questions/22987933/sas-to-count-excel-observations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!