<?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: Macro code for data import of multiple excel files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808635#M318853</link>
    <description>&lt;P&gt;If you have a data set with those variables using CALL EXECUTE is easier.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;

set formnames;

str = catt('proc import out=sasd._raw_', sas, 
 ' datafile= "',
  sas, 
  'd\', 
   oncore ,
   '.xlsx"', 
  'dbms=xlsx replace;',
   'Sheet = "',
   oncore,
    '";',
'Getnames = YES; run;'
  );

*call execute(str);
run;
length str $10000.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run this data set and make sure the code generated in the STR variable is the correct PROC IMPORT.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then once you're sure the code is correct, you can uncomment the CALL EXECUTE and that will run all the proc imports in the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you decide to continue with macro logic, look at this line.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; DATAFILE= "&amp;amp;sasd\&amp;amp;oncore.xlsx"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's attempting to reference a macro variable &lt;STRONG&gt;&amp;amp;sasd&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want it to use &amp;amp;sas you need to add a period after the sas to let SAS know that's the end of the macro variable, as well as add another period to separate the .XLSX portion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; DATAFILE= "&amp;amp;sas.d\&amp;amp;oncore..xlsx"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343190"&gt;@amb4kw&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to export data from a clinical database (OnCore) and analyze it in SAS. The database exports data as multiple excel files rather than one large dataset. This macro code is supposed to import all the the raw excel files into SAS and I cannot figure out why there is an error message within my code. It seems to have an issue when I specify the library name "sasd" for the file name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="screenshot1[132].png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70581iEAE24F607D5DD16E/image-size/large?v=v2&amp;amp;px=999" role="button" title="screenshot1[132].png" alt="screenshot1[132].png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO formnames();
%DO i=1 %TO &amp;amp;numforms;
data _null_;
set formnames;
if n= &amp;amp;i;
call symput('Oncore', TRIM(Oncore));
call symput('SAS', TRIM(SAS));
run;
%put &amp;amp;Oncore;
PROC IMPORT OUT= sasd._raw_&amp;amp;sas
&amp;nbsp;DATAFILE= "&amp;amp;sasd\&amp;amp;oncore.xlsx"
&amp;nbsp;DBMS=XLSX REPLACE;
&amp;nbsp;SHEET="&amp;amp;Oncore";
&amp;nbsp;GETNAMES=YES;
RUN;
%END;
%MEND;
%formnames();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 Apr 2022 17:35:25 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-04-19T17:35:25Z</dc:date>
    <item>
      <title>Macro code for data import of multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808576#M318837</link>
      <description>&lt;P&gt;I am trying to export data from a clinical database (OnCore) and analyze it in SAS. The database exports data as multiple excel files rather than one large dataset. This macro code is supposed to import all the the raw excel files into SAS and I cannot figure out why there is an error message within my code. It seems to have an issue when I specify the library name "sasd" for the file name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="screenshot1[132].png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70581iEAE24F607D5DD16E/image-size/large?v=v2&amp;amp;px=999" role="button" title="screenshot1[132].png" alt="screenshot1[132].png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO formnames();
%DO i=1 %TO &amp;amp;numforms;
data _null_;
set formnames;
if n= &amp;amp;i;
call symput('Oncore', TRIM(Oncore));
call symput('SAS', TRIM(SAS));
run;
%put &amp;amp;Oncore;
PROC IMPORT OUT= sasd._raw_&amp;amp;sas
&amp;nbsp;DATAFILE= "&amp;amp;sasd\&amp;amp;oncore.xlsx"
&amp;nbsp;DBMS=XLSX REPLACE;
&amp;nbsp;SHEET="&amp;amp;Oncore";
&amp;nbsp;GETNAMES=YES;
RUN;
%END;
%MEND;
%formnames();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2022 15:07:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808576#M318837</guid>
      <dc:creator>amb4kw</dc:creator>
      <dc:date>2022-04-19T15:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code for data import of multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808577#M318838</link>
      <description>&lt;P&gt;We need to see ALL of the log for this macro. Please do not show us parts of the log.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Also, please copy the log (ALL of it for this macro) as text, not as a screen capture, and then paste it into the window that appears when you click on the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Insert Log Icon in SAS Communities.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66171iFEC370B1DBF07B28/image-size/large?v=v2&amp;amp;px=999" role="button" title="Insert Log Icon in SAS Communities.png" alt="Insert Log Icon in SAS Communities.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, if you search the SAS Communities, you will most likely find working code to import multiple excel files, you shouldn't have to write your own code for this.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2022 15:17:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808577#M318838</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-19T15:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code for data import of multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808581#M318839</link>
      <description>&lt;P&gt;Your input dataset, formnames, probably does not have a variable named SAS.&amp;nbsp; Or if it does it is numeric and has missing values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is why this line put eleven spaces followed by a period into the macro variable SAS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symput('SAS', TRIM(SAS));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Do not use the older CALL SYMPUT() function (unless you really need to insert leading or trailing spaces into your macro variables).&amp;nbsp; Use the modern (less than 40 years old) CALL SYMPUTX() function instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set formnames(firstobs=&amp;amp;i obs=&amp;amp;i);
  call symputx('Oncore', oncore);
  call symputx('SAS', sas);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Apr 2022 15:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808581#M318839</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-19T15:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code for data import of multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808582#M318840</link>
      <description>&lt;P&gt;This is at least one major probl&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;&amp;nbsp;DATAFILE= "&amp;amp;sasd\&amp;amp;oncore.xlsx"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The macro processor using the . to indicate the end of a macro variable when combining with other text. So if oncore were "filename" then &amp;amp;oncore.xlsx&amp;nbsp; would resolve to "filenamexlsx" without the dot for the extension.&lt;/P&gt;
&lt;P&gt;So that piece of code needs to be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;&amp;nbsp;DATAFILE= "&amp;amp;sasd\&amp;amp;oncore..xlsx"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your messages would make me think that for one or more records in the data set Formnames that ONCORE is missing.&lt;/P&gt;
&lt;P&gt;If your code actually does read&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;if n= &amp;amp;i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you expect to get the result from the i numbered record in the data set I hope that you have a variable named n.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may also want to use Call SYMPUTX instead of Symput to remove leading and trailing blanks that might occur with default .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a bunch of names in a data set then look at CALL EXECUTE to generate lines of code from the data set instead of looping around record numbers of a data set that way.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2022 15:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808582#M318840</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-19T15:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code for data import of multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808635#M318853</link>
      <description>&lt;P&gt;If you have a data set with those variables using CALL EXECUTE is easier.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;

set formnames;

str = catt('proc import out=sasd._raw_', sas, 
 ' datafile= "',
  sas, 
  'd\', 
   oncore ,
   '.xlsx"', 
  'dbms=xlsx replace;',
   'Sheet = "',
   oncore,
    '";',
'Getnames = YES; run;'
  );

*call execute(str);
run;
length str $10000.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run this data set and make sure the code generated in the STR variable is the correct PROC IMPORT.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then once you're sure the code is correct, you can uncomment the CALL EXECUTE and that will run all the proc imports in the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you decide to continue with macro logic, look at this line.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; DATAFILE= "&amp;amp;sasd\&amp;amp;oncore.xlsx"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's attempting to reference a macro variable &lt;STRONG&gt;&amp;amp;sasd&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want it to use &amp;amp;sas you need to add a period after the sas to let SAS know that's the end of the macro variable, as well as add another period to separate the .XLSX portion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; DATAFILE= "&amp;amp;sas.d\&amp;amp;oncore..xlsx"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/343190"&gt;@amb4kw&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to export data from a clinical database (OnCore) and analyze it in SAS. The database exports data as multiple excel files rather than one large dataset. This macro code is supposed to import all the the raw excel files into SAS and I cannot figure out why there is an error message within my code. It seems to have an issue when I specify the library name "sasd" for the file name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="screenshot1[132].png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70581iEAE24F607D5DD16E/image-size/large?v=v2&amp;amp;px=999" role="button" title="screenshot1[132].png" alt="screenshot1[132].png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO formnames();
%DO i=1 %TO &amp;amp;numforms;
data _null_;
set formnames;
if n= &amp;amp;i;
call symput('Oncore', TRIM(Oncore));
call symput('SAS', TRIM(SAS));
run;
%put &amp;amp;Oncore;
PROC IMPORT OUT= sasd._raw_&amp;amp;sas
&amp;nbsp;DATAFILE= "&amp;amp;sasd\&amp;amp;oncore.xlsx"
&amp;nbsp;DBMS=XLSX REPLACE;
&amp;nbsp;SHEET="&amp;amp;Oncore";
&amp;nbsp;GETNAMES=YES;
RUN;
%END;
%MEND;
%formnames();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2022 17:35:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-for-data-import-of-multiple-excel-files/m-p/808635#M318853</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-04-19T17:35:25Z</dc:date>
    </item>
  </channel>
</rss>

