<?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: Problem to import several CSV files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311646#M67422</link>
    <description>&lt;P&gt;Many thanks. That works.&lt;/P&gt;
&lt;P&gt;However, I accounter one more problem. Some of the variables contain both numeric and string values (such as 1 2 3 C T). Thus, SAS considered as missing all the string values.&lt;/P&gt;</description>
    <pubDate>Tue, 15 Nov 2016 10:47:51 GMT</pubDate>
    <dc:creator>Demographer</dc:creator>
    <dc:date>2016-11-15T10:47:51Z</dc:date>
    <item>
      <title>Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311631#M67411</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have 30 CSV files into a folder (P:\mydirectory). I would like to import them at once into SAS. I listed them into a csv file(listfiles .csv), which has this format:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 48pt;" width="64" cellspacing="0" cellpadding="0" border="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD style="height: 15.0pt; width: 48pt;" width="64" height="20"&gt;Name_list&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD style="height: 15.0pt;" height="20"&gt;FileA.csv&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD style="height: 15.0pt;" height="20"&gt;
&lt;P&gt;FileB.csv&lt;/P&gt;
&lt;P&gt;etc.&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I tried this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc import datafile="P:\mydirectory\listfiles.csv" out=work.listcsv dbms=csv replace;
   getnames=yes;
run;


proc sql;
select name_list into :name separated by '*' from work.listcsv;
%let count2 = 30;
quit;

%macro import;
%do i = 1 %to 30;
%let j = %scan(&amp;amp;name,&amp;amp;i,*);
proc import out = &amp;amp;j datafile="P:\mydirectory\&amp;amp;j";
dbms=csv replace;
getnames = yes;
run;
%end;

%import;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And nothing happens. Can you please help me to find why?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 10:01:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311631#M67411</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2016-11-15T10:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311632#M67412</link>
      <description>&lt;P&gt;You are missing a %mend Statement?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 10:03:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311632#M67412</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-15T10:03:45Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311636#M67415</link>
      <description>&lt;P&gt;Right...&lt;/P&gt;
&lt;P&gt;But now it says that error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR: Libref '**the name of the file****' exceeds 8 characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my example, I change the name of the csv files, but they have more than 8 characters. Is this a mandatory condition?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 10:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311636#M67415</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2016-11-15T10:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311639#M67416</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Error refers to the name of the library which in SAS is mandatory to have maximum 8 characters.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 10:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311639#M67416</guid>
      <dc:creator>Loko</dc:creator>
      <dc:date>2016-11-15T10:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311643#M67419</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/33143"&gt;@Demographer&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Right...&lt;/P&gt;
&lt;P&gt;But now it says that error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR: Libref '**the name of the file****' exceeds 8 characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my example, I change the name of the csv files, but they have more than 8 characters. Is this a mandatory condition?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Valid SAS names for SAS objects (filerefs, librefs, variables, macros, datasets, catalogs, ...) can only consist of letters, digits and underlines. They must start with a letter or underline, and have a maximum length of 32; filerefs and librefs have a maximum length of 8.&lt;/P&gt;
&lt;P&gt;If you want to make your dataset names the same as the filenames, you have to remove the .csv extension and the path and make sure that the remainder adheres to the above conventions.&lt;/P&gt;
&lt;P&gt;You get your error because SAS tries to interpret the filename (before the dot) as a libname and make "csv" the dataset name.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 10:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311643#M67419</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-15T10:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311644#M67420</link>
      <description>&lt;P&gt;You can do all in one step, by creating the import steps with call execute. Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.listcsv;
infile 'P:\mydirectory\listfiles.csv' firstobs=2 truncover;
input name_list $100.;
name_short = scan(name_list,1,'.'); * remove the .csv;
call execute('proc import out=work.'!!name_short!!' datafile="P:\mydirectory\'!!trim(name_list)!!'" dbms=csv replace;');
call execute('getnames=yes; run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, for lack of input data. But you should get the picture.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: added a missing "v" in the dynamically created proc import dbms=cs&lt;STRONG&gt;v&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 10:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311644#M67420</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-15T10:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311645#M67421</link>
      <description>&lt;P&gt;Few comments:&lt;/P&gt;
&lt;P&gt;1) SAS list uses a blank as separator. (repalce the '*' into ' ')&lt;BR /&gt;&amp;nbsp; &amp;nbsp; change your code line: &amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; name_list &lt;SPAN class="token keyword"&gt;into&lt;/SPAN&gt; :name separated &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; &lt;STRONG&gt;&lt;SPAN class="token string"&gt;' '&lt;/SPAN&gt;&lt;/STRONG&gt; &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; work&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;listcsv&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;2) You are not using the count2 macro variable. You can delete this line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Maybe you wanted to get from sql the count of names. In this case:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Take out the %LET COUNT2 &amp;nbsp;out from the SQL step and assign its value as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;%let count2 = &amp;amp;sqlobs;&lt;/STRONG&gt; &amp;nbsp; /* sqlobs is a SQL system macro variable */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;then you could use it inside your macro, as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;STRONG&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt; i &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%to&lt;/SPAN&gt; &amp;amp;count2&lt;SPAN class="token punctuation"&gt;;  /* count2 used instead 30 */&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/STRONG&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen﻿&lt;/a&gt;&amp;nbsp;noticed, you missed the &lt;STRONG&gt;%mend import;&lt;/STRONG&gt; statement, to close the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4) As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser﻿&lt;/a&gt;&amp;nbsp;noticed, you need remove the .csv from the output table names,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; otherwise SAs will interpret the &lt;STRONG&gt;filea.csv&lt;/STRONG&gt; as table &lt;STRONG&gt;csv&lt;/STRONG&gt; in library &lt;STRONG&gt;filea, &lt;/STRONG&gt;which probably does not exist.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 10:50:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311645#M67421</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-15T10:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311646#M67422</link>
      <description>&lt;P&gt;Many thanks. That works.&lt;/P&gt;
&lt;P&gt;However, I accounter one more problem. Some of the variables contain both numeric and string values (such as 1 2 3 C T). Thus, SAS considered as missing all the string values.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 10:47:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311646#M67422</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2016-11-15T10:47:51Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311648#M67423</link>
      <description>&lt;P&gt;Since the individual files are imported with proc import, add a suitable guessingrows statement in the "call execute"d code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If all your csv files in that directory share the same structure, you could use a tailored data step in place of proc import to read them.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 11:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311648#M67423</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-15T11:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311649#M67424</link>
      <description>&lt;P&gt;I tried adding GUESSINGROWS=100;&lt;/P&gt;
&lt;P&gt;And I have the followed error message:&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;All my CSV are in the same directory and have the same structure, but I am not really sure what should I do to use a tairloed data step properly with csv.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 11:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311649#M67424</guid>
      <dc:creator>Demographer</dc:creator>
      <dc:date>2016-11-15T11:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: Problem to import several CSV files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311654#M67426</link>
      <description>&lt;P&gt;You need to add it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.listcsv;
infile 'P:\mydirectory\listfiles.csv' firstobs=2 truncover;
input name_list $100.;
name_short = scan(name_list,1,'.'); * remove the .csv;
call execute('proc import out=work.'!!name_short!!' datafile="P:\mydirectory\'!!trim(name_list)!!'" dbms=csv replace;');
call execute('getnames=yes; guessingrows=100; run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can extract the data step from the log. With DBMS=CSV, proc import creates a data step and runs that to read the data. The data step is written to the log and can be used as a blueprint for your own efforts.&lt;/P&gt;
&lt;P&gt;Basically, a data step for csv files looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data outfile;
infile "infile" dlm=',' firstobs=2 truncover lrecl=number;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;truncover means that the data step will not skip into the next line if not enough columns are present in any given line&lt;/P&gt;
&lt;P&gt;firstobs tells the data step to start in the 2nd line&lt;/P&gt;
&lt;P&gt;lrecl=&lt;EM&gt;number&lt;/EM&gt; sets a size for the input buffer (necessary if lines are longer than 254 bytes)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;attrib
  var1 length= format=
  var2
  ...
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;define attributes like length, format, informat for your variables&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input
  var1
  var2
  ...
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;read variables&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;end data step&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 11:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-to-import-several-CSV-files/m-p/311654#M67426</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-15T11:46:06Z</dc:date>
    </item>
  </channel>
</rss>

