<?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: Import Macro? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795820#M255303</link>
    <description>&lt;P&gt;That's why using Excel files as data source is always a BAD IDEA. Save the sheets to csv files (or require such in the first place), so you can read them all with the same DATA step.&lt;/P&gt;</description>
    <pubDate>Sat, 12 Feb 2022 06:37:56 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-02-12T06:37:56Z</dc:date>
    <item>
      <title>Import Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795805#M255289</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I use a macro to import 8 Excel files with 5 spreadsheets, then I combine all the sheets based on the different years.&amp;nbsp; &amp;nbsp;I found that I could not do the merge due to lots of variables are not in the same format.&amp;nbsp; Please see the sample codes and error messages below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Y1=1617;
%let Y2=1718;
%let Y3=1819;
%let X1=VAN;
%let X2=ROC;
%let X3=CIN;

%let X8=PIT;

%macro import;

%do i  = 1 %to 3;

 %do z  = 1 %to 3;

PROC IMPORT OUT=&amp;amp;&amp;amp;X&amp;amp;i.._&amp;amp;&amp;amp;Y&amp;amp;z.
			datafile="Pathwasy\Underly_Con_&amp;amp;&amp;amp;X&amp;amp;i.."
			dbms=xlsx replace;
			sheet=&amp;amp;&amp;amp;X&amp;amp;i.._&amp;amp;&amp;amp;Y&amp;amp;z..;
run;

%end; 
%end; 
%mend;
%import;

data underly_1617;
	set cin_1617 hou_1617 kc_1617 pit_1617 roc_1617 sea_1617 van_1617;
run;

proc sort data=underly_1617; by studysite caseid; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Brief error messages are shown below, more than twenty variables had both character and numeric issues when I tried to merge in the last step.&amp;nbsp; &amp;nbsp;I am curious whether I should format those variables into numeric while I did the macro import; or get around of both character and numeric issue while I combine the subset.&amp;nbsp; &amp;nbsp;Please advise, thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;70 data underly_1617;&lt;BR /&gt;71 set cin_1617 roc_1617&amp;nbsp; van_1617;&lt;BR /&gt;ERROR: Variable uapnea has been defined as both character and numeric.&lt;BR /&gt;ERROR: Variable ugerd has been defined as both character and numeric.&lt;BR /&gt;ERROR: Variable uprem has been defined as both character and numeric.&lt;BR /&gt;ERROR: Variable ugestage has been defined as both character and numeric.&lt;BR /&gt;72 run;&lt;/P&gt;
&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;WARNING: The data set WORK.UNDERLY_1617 may be incomplete. When this step was stopped there were 0&lt;BR /&gt;observations and 49 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.03 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Feb 2022 00:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795805#M255289</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2022-02-12T00:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Import Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795806#M255290</link>
      <description>&lt;P&gt;The best way is to not use XLSX files as your data source.&amp;nbsp; Spreadsheets are not databases. Each cell is independent.&lt;/P&gt;
&lt;P&gt;Either use a SAS datasets or some other format that actually creates structured data.&lt;/P&gt;
&lt;P&gt;Or use a CSV file or other text files and write your own data step(s) to read the files so that you have control.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might also want to look into why variables are getting defined with the wrong type.&amp;nbsp; What is the correct type for the variable?&amp;nbsp; If it is supposed to be character make sure all of the cells in that column contain text instead of numbers. If the issue is just a couple of cells it might faster make a copy of the XLSX file and clean it up before trying to import it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might try exporting the sheets to CSV files yourself and then using data steps to read them.&lt;/P&gt;
&lt;P&gt;If that is not workable you might try taking the datasets you got from PROC IMPORT and writing them out as CSV files from SAS.&amp;nbsp; That has the advantage of being programmable.&amp;nbsp; &amp;nbsp;But then you need to make sure the columns in the sheets are in the same order.&amp;nbsp; But if the PROC IMPORT step has caused the values to be read wrong you might not notice the change For example if you have a date cell in a column that needs to be character in SAS then you get a digit string that represents the internal number that EXCEL uses for that date instead of something a human would recognize as a date.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Feb 2022 00:40:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795806#M255290</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-12T00:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: Import Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795820#M255303</link>
      <description>&lt;P&gt;That's why using Excel files as data source is always a BAD IDEA. Save the sheets to csv files (or require such in the first place), so you can read them all with the same DATA step.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Feb 2022 06:37:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795820#M255303</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-12T06:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: Import Macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795836#M255312</link>
      <description>&lt;P&gt;As others have said, Excel is bad. : )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, if you want to use the XLSX engine, I think you will be stuck doing something like:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Import a sheet&lt;/LI&gt;
&lt;LI&gt;Check the variable types (using proc contents or whatever)&lt;/LI&gt;
&lt;LI&gt;Use a DATA step to change the variable types to what you want them to be.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;That can all be automated, but it's a hassle.&amp;nbsp; The problem is that Excel doesn't have variable types, so SAS needs to look at the values in a column of cells and guess at a variable type.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This sort of importing work would be much easier if XLSX engine supported the DBSASTYPE option.&amp;nbsp; Then you could use the option to specify the variable type to be used when reading into SAS.&amp;nbsp; I submitted a ballot item with that suggestion.&amp;nbsp; If you agree, please upvote it:&amp;nbsp;&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SASware-Ballot-Ideas/Add-DBSASTYPE-or-similar-option-to-XLSX-engine-to-force-variable/idi-p/453938" target="_blank"&gt;https://communities.sas.com/t5/SASware-Ballot-Ideas/Add-DBSASTYPE-or-similar-option-to-XLSX-engine-to-force-variable/idi-p/453938&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can use the older EXCEL or PCFILES engines, they do support the DBSASTYPE option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Feb 2022 13:11:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Macro/m-p/795836#M255312</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-02-12T13:11:03Z</dc:date>
    </item>
  </channel>
</rss>

