<?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: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497101#M131641</link>
    <description>&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;Thank you for being so kind and patient.&amp;nbsp;I am extremely grateful!&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;Patrick&lt;/P&gt;</description>
    <pubDate>Wed, 19 Sep 2018 19:41:38 GMT</pubDate>
    <dc:creator>DrSolverson</dc:creator>
    <dc:date>2018-09-19T19:41:38Z</dc:date>
    <item>
      <title>Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497073#M131627</link>
      <description>&lt;P&gt;Hi! Using SAS 9.4 here. I have large data sets from a recent experiment: my subjects have ~18 hours of minute-to-minute measurements summarized in excel spreadsheets. 36 subjects and 4 treatments. I came across this nice macro in the user guide that allows me to read all the data into SAS:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext); 
   %local cnt filrf rc did memcnt name; 
   %let cnt=0;          

   %let filrf=mydir;    
   %let rc=%sysfunc(filename(filrf,&amp;amp;dir)); 
   %let did=%sysfunc(dopen(&amp;amp;filrf));
    %if &amp;amp;did ne 0 %then %do;   
   %let memcnt=%sysfunc(dnum(&amp;amp;did));    

    %do i=1 %to &amp;amp;memcnt;              
                       
      %let name=%qscan(%qsysfunc(dread(&amp;amp;did,&amp;amp;i)),-1,.);                    
                    
      %if %qupcase(%qsysfunc(dread(&amp;amp;did,&amp;amp;i))) ne %qupcase(&amp;amp;name) %then %do;
       %if %superq(ext) = %superq(name) %then %do;                         
          %let cnt=%eval(&amp;amp;cnt+1);       
          %put %qsysfunc(dread(&amp;amp;did,&amp;amp;i));  
          proc import datafile="&amp;amp;dir\%qsysfunc(dread(&amp;amp;did,&amp;amp;i))" out=dsn&amp;amp;cnt  (drop = date
		  time_elapsed)
           dbms=xlsx replace;            
          run;          
       %end; 
      %end;  

    %end;
      %end;
  %else %put &amp;amp;dir cannot be open.;
  %let rc=%sysfunc(dclose(&amp;amp;did));      
             
 %mend drive;
 
%drive(C:\Users\mydirectory\,xlsx) &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the problem is&amp;nbsp;it renames all the spreadsheets that it reads into SAS - in proc import, the output for each spreadsheet is assigned the name "dsn&amp;amp;cnt" - Will be confusing moving forward to lose my unique identifier. I was hoping someone would know how to rewrite this code so that it keeps the name of the original filename that was read into SAS.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Further, I've figured out how to drop the variables I don't want in the final SAS spreadsheet, but I'm still figuring out how to merge all 144 files into one spreadsheet, while somehow creating a subject and treatment ID for each&amp;nbsp;merged file worth of data so I can begin performing summary statistics.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated!&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Patrick&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 17:54:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497073#M131627</guid>
      <dc:creator>DrSolverson</dc:creator>
      <dc:date>2018-09-19T17:54:04Z</dc:date>
    </item>
    <item>
      <title>Re: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497075#M131628</link>
      <description>&lt;P&gt;You need to provide more information to get good help.&lt;/P&gt;
&lt;P&gt;Do all of the Excel files have just one sheet?&amp;nbsp; Or do they have multiple sheets?&lt;/P&gt;
&lt;P&gt;Do all of the Excel sheets have the same variables (columns)?&lt;/P&gt;
&lt;P&gt;Is all of the data in contents of the sheets&amp;nbsp; or is some of it hiding in the filename, sheetname or column name?&amp;nbsp; For example is the subject id and date exist as columns in the sheet like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Subject Date Var1 Var2 Var3
101 2018/09/20 1 2 3&lt;/PRE&gt;
&lt;P&gt;Or do they only exist in the filename or sheetname?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 18:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497075#M131628</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-19T18:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497079#M131629</link>
      <description>&lt;P&gt;Hi Tom, thanks for the speedy reply:&lt;/P&gt;&lt;P&gt;Do all of the Excel files have just one sheet?&amp;nbsp; Or do they have multiple sheets?&lt;/P&gt;&lt;P&gt;-Just one sheet per file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do all of the Excel sheets have the same variables (columns)?&lt;/P&gt;&lt;P&gt;-Yes, the columns are identical. However, the number of rows are different between subject files, accounting for differences in time (minute to minute measures varied by a few hours; somewhere between 900 to 1200 rows of data). The macro didn't appear to have a problem with this difference.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Layout of output" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23385iEA14610E718CFD7C/image-size/large?v=v2&amp;amp;px=999" role="button" title="output spreadsheet.png" alt="Layout of output" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Layout of output&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is all of the data in contents of the sheets&amp;nbsp; or is some of it hiding in the filename, sheetname or column name?&lt;/P&gt;&lt;P&gt;-subject ID is the filename (unique run number, to be more specific). Would be nice to add a column for subject ID if there's a quick way to do that, gleaning from file name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps hone in on my problem!&lt;/P&gt;&lt;P&gt;Patrick&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 18:20:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497079#M131629</guid>
      <dc:creator>DrSolverson</dc:creator>
      <dc:date>2018-09-19T18:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497083#M131632</link>
      <description>&lt;P&gt;Do all of them name the sheet as SHEET1, like in your photograph?&lt;/P&gt;
&lt;P&gt;Can you use the XSLX libname engine to read one of the files?&amp;nbsp; If so it is generally easier to use than PROC IMPORT.&amp;nbsp; But your column headers might cause trouble. Try it on one of the files.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname in xlsx 'filename.xlsx';
data test;
 set in.sheet1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your example macro is a little more complex than it needs to be.&amp;nbsp; For example why does it keep calling DREAD() over and over again instead of just remembering the value?&amp;nbsp; It should be possible to pull out the filename and use that for the name of the dataset. As long as the filenames are valid SAS names. 32 characters or less. Contain letters, digits and underscores, Starts with letter or underscore.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 18:32:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497083#M131632</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-19T18:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497087#M131634</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;Do all of them name the sheet as SHEET1, like in your photograph?&lt;/P&gt;&lt;P&gt;-yes, they all say sheet1. This was output recorded from a matlab program into an excel spreadsheet. Only difference across the files will be the length (rows) of the data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you use the XSLX libname engine to read one of the files?&lt;/P&gt;&lt;P&gt;-Yes, that worked just fine. Do you have a do loop in mind?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="xlsx libname engine.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23387i89738E7E7A15884B/image-size/large?v=v2&amp;amp;px=999" role="button" title="xlsx libname engine.png" alt="xlsx libname engine.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regarding your macro questions, you'll have to take that up with someone more enlightened!&lt;/P&gt;&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.3&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0ctmldxf23ixtn1kqsoh5bsgmg8.htm&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.3&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=n0ctmldxf23ixtn1kqsoh5bsgmg8.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 18:53:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497087#M131634</guid>
      <dc:creator>DrSolverson</dc:creator>
      <dc:date>2018-09-19T18:53:16Z</dc:date>
    </item>
    <item>
      <title>Re: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497089#M131635</link>
      <description>&lt;P&gt;Try something like this.&amp;nbsp; Untested.&lt;/P&gt;
&lt;P&gt;I added logic to split the filename into BASE_NAME and NAME_EXT parts.&amp;nbsp; I hard coded the test to just look for XLSX files.&lt;/P&gt;
&lt;P&gt;I added input parameter for the name of the dataset you want to create.&lt;/P&gt;
&lt;P&gt;It will read in each sheet and add a variable SUBJECT to hold the name of the xlsx file. Then it will use PROC APPEND to make one output dataset.&amp;nbsp; I remove the DROP= option you had. You could add that back if you wanted .&amp;nbsp; Personally I would use a normal DROP statement instead of the DROP= dataset option.&lt;/P&gt;
&lt;P&gt;I added a PROC FREQ to show how many records were loaded from each sheet.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,out);
%local cnt filrf rc did name name_ext base_name;
%let cnt=0;

%let filrf=mydir;
%let rc=%sysfunc(filename(filrf,&amp;amp;dir\));
%let did=%sysfunc(dopen(&amp;amp;filrf));
%if &amp;amp;did ne 0 %then %do;
  %if %sysfunc(exist(&amp;amp;out)) %then %do;
    proc delete data=&amp;amp;out; run;
  %end;
  %do i=1 %to %sysfunc(dnum(&amp;amp;did));
    %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));
    %if %index(&amp;amp;name,.) &amp;gt; 1 %then %do;
      %let name_ext=%qupcase(%qscan(&amp;amp;name,-1,.));
      %let base_name=%qsubstr(&amp;amp;name,1,%index(&amp;amp;name,.)-1);
    %end;
    %else %do;
      %let name_ext=;
      %let base_name=&amp;amp;name ;
    %end;
    %if XLSX = &amp;amp;name_ext %then %do;
      %let cnt=%eval(&amp;amp;cnt+1);
      libname in xlsx "&amp;amp;dir\&amp;amp;name" ;
      data for_loading;
        length subject $32 ;
        subject="&amp;amp;base_name";
        set in.sheet1 ;
      run;
      proc append base=&amp;amp;out data=for_loading force;
      run;
    %end;
  %end;
  %put NOTE: Read &amp;amp;cnt worksheets from &amp;amp;dir into &amp;amp;out.. ;
   proc freq data=&amp;amp;out;
      tables subject ;
   run;
  %let rc=%sysfunc(dclose(&amp;amp;did));
%end;
%else %put ERROR: &amp;amp;dir cannot be opened.;
%let rc=%sysfunc(filename(filrf));
%mend drive;

%drive(dir=C:\Users\mydirectory,out=mydata)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:12:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497089#M131635</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-19T19:12:59Z</dc:date>
    </item>
    <item>
      <title>Re: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497096#M131637</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;It worked so well I'm borderline emotional (a biologist writing SAS code is a tormented creature).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As mentioned, each run had a varying length of time, and it will make life easier to clip off the ends so everyone has uniform data (for example, only moving forward with 5:00 PM to 9:48 AM data) - truncated to the shortest dataset, instead of extrapolating into the unknown. Is there a way to add this to your macro?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since we've moved away from proc import, how do I drop unwanted columns? For example, I don't wan't to include Date, time elapsed, etc. There's really only 4 columns of data I want to move forward with.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:19:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497096#M131637</guid>
      <dc:creator>DrSolverson</dc:creator>
      <dc:date>2018-09-19T19:19:39Z</dc:date>
    </item>
    <item>
      <title>Re: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497099#M131639</link>
      <description>&lt;P&gt;If you want to subset the data then just modify the data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;     data for_loading;
        length subject $32 ;
        subject="&amp;amp;base_name";
        set in.sheet1 ;
      run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could add DROP and/or KEEP statement to tell it which variables to keep.&lt;/P&gt;
&lt;P&gt;You could add an IF or WHERE statement to tell it which observations to keep.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:31:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497099#M131639</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-19T19:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: Reading several xlsx spreadsheets into SAS with macro - how do I keep the original filename?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497101#M131641</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;Thank you for being so kind and patient.&amp;nbsp;I am extremely grateful!&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;Patrick&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 19:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-several-xlsx-spreadsheets-into-SAS-with-macro-how-do-I/m-p/497101#M131641</guid>
      <dc:creator>DrSolverson</dc:creator>
      <dc:date>2018-09-19T19:41:38Z</dc:date>
    </item>
  </channel>
</rss>

