No BSD License  

Highlights from
Export data to SAS

image thumbnail
from Export data to SAS by Dimitri Shvorob
(via Excel; with SAS/ACCESS and ActiveX Automation Server)

saswrite(array,sasfile,xlsfile,varargin)
function saswrite(array,sasfile,xlsfile,varargin)
% SASWRITE Write numeric data to a SAS dataset
% INPUTS   array   - numeric array
%          sasfile - full path to input SAS dataset 
%          xlsfile - full path to Excel workbook containing 'SAS2Excel' macro 
% NOTES    SASWRITE invokes XLSWRITE to write array to an Excel workbook, and 
%          uses Microsoft Excel to open an instance of SAS and execute IMPORT
%          procedure, transferring Excel data to a SAS dataset.  Both SAS and
%          Excel are needed to run SASWRITE.  Excel workbook with 'SAS2Excel'
%          macro must be retained, its path provided in xlsfile. 'C:\sas.xls'
%          is used for temporary data storage; if you don't have write access
%          to disk C, edit  all references to 'C:\sas.xls' in this m-file and
%          'SAS2Excel' macro. You will need to close any open SAS session.
% EXAMPLE  See SASWRITEDEMO
% SEE ALSO SASREAD (companion File Exchange submission), XLSWRITE
% AUTHOR   Dimitri Shvorob, dimitri.shvorob@vanderbilt.edu, 11/1/05

f = 'C:\sas.xls';
if nargin < 1
   error('Input argument ''array'' is undefined')
end
if nargin < 2
   error('Input argument ''sasfile'' is undefined')
end
if nargin < 3
   error('Input argument ''xlsfile'' is undefined')
end
if nargin > 3
   names = varargin{1};
   if ~iscellstr(names)
      error('Invalid syntax: ''names'' must be a cell array of strings')
   end
else
   names = {}; 
end
if ~exist(xlsfile,'file') 
   error('Write failed: could not find ''xlsfile''')
end   
if exist(sasfile,'file')
   error('Output file ''sasfile'' already exists')
end   
n = length(names);
try
    if exist(f,'file')
       delete(f)
       disp('deleted')
    end   
catch
   error(['File ' f ', to be used for data transfer, already exists and could not be deleted']) 
end   
try
   e = actxserver('Excel.Application');
catch
   error('Write failed: could not start Excel')
end    
try
   b = e.Workbooks.Open(xlsfile);
catch
   e.Quit
   error('Write failed: could not open ''xlsfile''')
end   
try
   s = b.Sheets.get('Item',1);
   c = s.get('Cells',1,1); 
   m = findstr(sasfile,'.');
   if isempty(m) 
      c.Value = sasfile;
   else
      c.Value = sasfile(1:m-1);
   end   
   c = s.get('Cells',2,1);  c.Value = n;
   for i = 1:n
       c = s.get('Cells',i+2,1); 
       c.Value = char(names{i});
   end
catch
   e.Quit
   error('Write failed: ''xlsfile'' may have been corrupted')
end   
try 
   i = xlswrite(f,[zeros(1,size(array,2)); array]);  %#ok
catch 
   e.Quit 
   error('Write failed: could not create temporary workbook')
end
try    
   e.ExecuteExcel4Macro('!Excel2SAS()');
catch
   e.Quit
   delete(f)
   error('Write failed: SAS session terminated without creating temporary workbook')
end
b.Close
e.Quit
delete(f)

Contact us at files@mathworks.com