<?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: Importing entire library of CSV datasets into SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347204#M80161</link>
    <description>&lt;P&gt;There's a solution I've been working on here, this was a question on here a few weeks ago (14 days to be exact).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Apr 2017 22:52:35 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-04-04T22:52:35Z</dc:date>
    <item>
      <title>Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347159#M80149</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a library of around 50 CSV datasets. I would like to import them all in one step (if possible) rather than importing individual files one by one with proc import. &amp;nbsp;Can anyone help me with this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: In other words, I am trying to avoid&amp;nbsp;doing this 50 times:&lt;/P&gt;
&lt;P&gt;PROC IMPORT OUT= WORK.dataset1&lt;BR /&gt; DATAFILE= "Path\dataset1.csv" &lt;BR /&gt; DBMS=CSV REPLACE;&lt;BR /&gt; GETNAMES=YES;&lt;BR /&gt; DATAROW=2; &lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;MK&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 19:23:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347159#M80149</guid>
      <dc:creator>MiraKr_</dc:creator>
      <dc:date>2017-04-04T19:23:26Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347162#M80150</link>
      <description>&lt;P&gt;What do you mean by a library of CSV files? &amp;nbsp;If you have 50 CSV files that represent different sets of records of the same data then you should be able to read it with one data step. &amp;nbsp;If you have 50 individual files with their own unique structure then you will need to read them each separately. &amp;nbsp;It should be possible to make the computer generate the 50 PROC IMPORT steps for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both of those situations are common questions on this forum. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 19:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347162#M80150</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-04T19:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347166#M80151</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi--,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I have 50 individual files with their own unique structure and names in one folder (please see attached photo). Each uque datasets have different variables in it. How can we set up the SAS code to repeat the import procedure 50 or so times?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;MK&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13670i8419697274B8B1F5/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Capture1.JPG" title="Capture1.JPG" /&gt;</description>
      <pubDate>Tue, 04 Apr 2017 19:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347166#M80151</guid>
      <dc:creator>MiraKr_</dc:creator>
      <dc:date>2017-04-04T19:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347193#M80157</link>
      <description>&lt;P&gt;Something like below should do.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=C:\test;

Filename filelist pipe "dir /b &amp;amp;path\*.csv";

Data dirList;
  Infile filelist truncover;
  Input filename $100.;
  Put filename=;
Run;

filename codegen temp;
data _null_;
/*  file print;*/
  file codegen;
  set dirList;
  put 
    / "PROC IMPORT OUT= WORK.dataset_" _n_ z2.
    / "  DATAFILE= '&amp;amp;path\" filename "'" 
    / "  DBMS=CSV REPLACE;"
    / "  GETNAMES=YES;"
    / "  DATAROW=2;" 
    / "  guessingrows=max;"
    / "RUN;"
    ;
run;

%include codegen / source2;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also use CALL EXECUTE() instead of writing the code to a temporary file and then execute it via an %INCLUDE statement.&lt;/P&gt;
&lt;P&gt;I personally prefer the approach using a temporary file as this allows me to create code which I can easily examine during development without execution by writing it to PRINT.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2017 21:44:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347193#M80157</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-04-05T21:44:50Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347197#M80158</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Importing entire directory of XLS file into SAS

This requires full SAS so if yo get a error you may
want to contact your IT dept and show them the error.

This does not solve a CSV problem because it deals with
a directory of excel files. I tried to use 'proc import'
but it crashed SAS. DM commands were ignored.

You have much more flexibilty with excel files.

HAVE
====

 Directory of d:\xls

  cars.xlsx    04/04/2017  04:53 PM  49,376
  class.xlsx   04/04/2017  04:53 PM   9,253
  iris.xlsx    04/04/2017  04:53 PM  12,870

WANT
====

   WORK.CARS
   WORK.CLASS
   WORK.IRIS

WORKING CODE
============

        rc=filename("mydir","d:\xls");
        did=dopen("mydir");

        rc=dosubl('
           libname xel  "d:/xls/&amp;amp;mem..xlsx";
           data &amp;amp;mem;
             set xel.&amp;amp;mem;

*                _                  _       _
 _ __ ___   __ _| | _____        __| | __ _| |_ __ _
| '_ ` _ \ / _` | |/ / _ \_____ / _` |/ _` | __/ _` |
| | | | | | (_| |   &amp;lt;  __/_____| (_| | (_| | || (_| |
|_| |_| |_|\__,_|_|\_\___|      \__,_|\__,_|\__\__,_|

;


CREATE A DIRECTORY OF EXCEL FILES
=================================

dm "dexport sashelp.class 'd:\xls\class.xlsx' replace";
dm "dexport sashelp.cars  'd:\xls\cars.xlsx' replace";
dm "dexport sashelp.iris  'd:\xls\iris.xlsx' replace";


*          _       _   _
 ___  ___ | |_   _| |_(_) ___  _ __
/ __|/ _ \| | | | | __| |/ _ \| '_ \
\__ \ (_) | | |_| | |_| | (_) | | | |
|___/\___/|_|\__,_|\__|_|\___/|_| |_|

;

%symdel mem / nowarn;
data _null_;

   rc=filename("mydir","d:\xls");
   did=dopen("mydir");
   if did &amp;gt; 0 then do;
        memcount=dnum(did);
        do i=1 to memcount;
           name=dread(did,i);
           call symputx('mem',scan(name,1,'.'));
           put name=;
           rc=dosubl('
              libname xel  "d:/xls/&amp;amp;mem..xlsx";
              data &amp;amp;mem;
                set xel.&amp;amp;mem;
              run;quit;
              libname xel clear;
           ');
        end;
    end;
    stop;

run;quit;

NAME=cars.xlsx
NOTE: Libref XEL was successfully assigned as follows:
      Engine:        EXCEL
      Physical Name: d:/xls/cars.xlsx
NOTE: There were 428 observations read from the data set XEL.cars.
NOTE: The data set WORK.CARS has 428 observations and 15 variables.
NOTE: DATA statement used (Total process time):
      real time           0.06 seconds
      cpu time            0.07 seconds


NOTE: Libref XEL has been deassigned.
NAME=class.xlsx
NOTE: Libref XEL was successfully assigned as follows:
      Engine:        EXCEL
      Physical Name: d:/xls/class.xlsx
NOTE: There were 19 observations read from the data set XEL.class.
NOTE: The data set WORK.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


NOTE: Libref XEL has been deassigned.
NAME=iris.xlsx
NOTE: Libref XEL was successfully assigned as follows:
      Engine:        EXCEL
      Physical Name: d:/xls/iris.xlsx
NOTE: There were 150 observations read from the data set XEL.iris.
NOTE: The data set WORK.IRIS has 150 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds


NOTE: Libref XEL has been deassigned.
NOTE: DATA statement used (Total process time):
      real time           0.55 seconds
      cpu time            0.35 seconds

48  !     quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2017 22:31:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347197#M80158</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-04-04T22:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347204#M80161</link>
      <description>&lt;P&gt;There's a solution I've been working on here, this was a question on here a few weeks ago (14 days to be exact).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 22:52:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347204#M80161</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-04T22:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347429#M80248</link>
      <description>&lt;P&gt;Hi Patrick,&lt;/P&gt;
&lt;P&gt;Thank you for the code! It works like magic. The only thing I would like to edit would be to assign the original CSV dataset names (listed in Dirlist) to the newly created SAS datasets, rather than creating SAS datasets with names dataset_01, dataset_02, etc, which is not really informative. I will try to work on that, but if you have an easy solution, please let me know.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you all for posting solutions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MK&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2017 15:28:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347429#M80248</guid>
      <dc:creator>MiraKr_</dc:creator>
      <dc:date>2017-04-05T15:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347524#M80291</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12283"&gt;@MiraKr_&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here you go:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=C:\temp;

filename filelist pipe "dir /b &amp;amp;path\*.csv";

data dirList;
  infile filelist truncover;
  input CSVfilename $100.;
  length SASTableName $32;
  SASTableName=substrn(scan(CSVfilename,1,'.'),1,31);

  /* valid SAS table name? */
  if not nvalid(SASTableName) then
    do;
      /* covert string to valid SAS table name */
      SASTableName=prxchange('s/^(\d)|\W+/_\1/oi',-1,strip(SASTableName));
    end;
run;


filename codegen temp;
data _null_;
/*  file print;*/
  file codegen;
  set dirList;
  put 
    / "PROC IMPORT OUT= WORK." SASTableName
    / "  DATAFILE= '&amp;amp;path\" CSVfilename +(-1) "'" 
    / "  DBMS=CSV REPLACE;"
    / "  GETNAMES=YES;"
    / "  DATAROW=2;" 
    / "  guessingrows=max;"
    / "RUN;"
    ;
run;

%include codegen / source2;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What's not covered in above code:&lt;/P&gt;
&lt;P&gt;1. substring of 32 characters with all non-valid characters replaced with underscore could lead to duplicate table names (unlikely but possible)&lt;/P&gt;
&lt;P&gt;2. Proc Import with DATAROW=2 will throw an error in case of an empty .csv source file&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2017 21:50:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/347524#M80291</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-04-05T21:50:05Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/348692#M80769</link>
      <description>&lt;P&gt;This solution is very short and easy to run, it converted all 182 csv datasets into sas datasets. Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 13:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/348692#M80769</guid>
      <dc:creator>MiraKr_</dc:creator>
      <dc:date>2017-04-10T13:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/348694#M80770</link>
      <description>Thank you</description>
      <pubDate>Mon, 10 Apr 2017 13:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/348694#M80770</guid>
      <dc:creator>MiraKr_</dc:creator>
      <dc:date>2017-04-10T13:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Importing entire library of CSV datasets into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/348696#M80771</link>
      <description>Thank you!</description>
      <pubDate>Mon, 10 Apr 2017 13:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-entire-library-of-CSV-datasets-into-SAS/m-p/348696#M80771</guid>
      <dc:creator>MiraKr_</dc:creator>
      <dc:date>2017-04-10T13:40:19Z</dc:date>
    </item>
  </channel>
</rss>

