<?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 multiple CSV files and create one SAS dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668421#M200361</link>
    <description>&lt;P&gt;You can use wildcards in the INFILE statement, or read a set of filenames from datalines, see here:&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&amp;amp;locale=en#n1x9joj5qbfkjyn1kqtm7joru0qb" target="_blank" rel="noopener"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&amp;amp;locale=en#n1x9joj5qbfkjyn1kqtm7joru0qb&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 10 Jul 2020 17:28:58 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-07-10T17:28:58Z</dc:date>
    <item>
      <title>Import multiple CSV files and create one SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668416#M200358</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a beginner and started using SAS for a few months. I've got a problem and would like to use the help and advice of the SAS experts here.&lt;/P&gt;&lt;P&gt;I have multiple CSV data files with the same set of variables in every raw data file (also all the files are present in the same directory). I would like to import all of them once and create a new SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for all your help in Advance!&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 17:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668416#M200358</guid>
      <dc:creator>DivyaGadde</dc:creator>
      <dc:date>2020-07-10T17:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: Import multiple CSV files and create one SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668421#M200361</link>
      <description>&lt;P&gt;You can use wildcards in the INFILE statement, or read a set of filenames from datalines, see here:&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&amp;amp;locale=en#n1x9joj5qbfkjyn1kqtm7joru0qb" target="_blank" rel="noopener"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&amp;amp;locale=en#n1x9joj5qbfkjyn1kqtm7joru0qb&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 17:28:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668421#M200361</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-10T17:28:58Z</dc:date>
    </item>
    <item>
      <title>Re: Import multiple CSV files and create one SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668425#M200365</link>
      <description>&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-text-files-that-have/ta-p/223627" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-text-files-that-have/ta-p/223627&lt;/A&gt;</description>
      <pubDate>Fri, 10 Jul 2020 17:45:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668425#M200365</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-10T17:45:05Z</dc:date>
    </item>
    <item>
      <title>Re: Import multiple CSV files and create one SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668430#M200368</link>
      <description>&lt;P&gt;Since CSV files are just text there is no need to "import" them.&amp;nbsp; Just read them.&lt;/P&gt;
&lt;P&gt;If you really don't know what the structure is you could use PROC IMPORT to read one and then use the code it generates to help you writing code to read the file.&amp;nbsp; But it writes really ugly code over complicated code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to the multiple file issue having all of them in one directory helps. Especially if you want to read ALL of the CSV files in that directory. In that case you can use a * wildcard in the file name of the INFILE statement.&amp;nbsp; The only tricky part will be if the files have header lines.&amp;nbsp; For that you can use the FILENAME= option of the INFILE statement.&amp;nbsp; That is also useful if there is information in the name of the file that is not already in the file.&amp;nbsp; Like a date or company name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   length fname $200 ;
   infile 'c:\mydir\*.csv' dsd dlm=',' truncover filename=fname;
   input @;
   if fname ne lag(fname) then input;
   input var1 var2 .... ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Jul 2020 18:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668430#M200368</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-10T18:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Import multiple CSV files and create one SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668447#M200375</link>
      <description>&lt;P&gt;Thank you for your quick response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do I have to list out all the variables in the input statement?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 19:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668447#M200375</guid>
      <dc:creator>DivyaGadde</dc:creator>
      <dc:date>2020-07-10T19:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: Import multiple CSV files and create one SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668454#M200378</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/233523"&gt;@DivyaGadde&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your quick response.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do I have to list out all the variables in the input statement?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes and no.&amp;nbsp; You need to write a data step that reads all of the variables.&amp;nbsp; But you can use short cuts so that the actual INPUT statement does not need to list each variable name.&amp;nbsp; One way is if you have a series of variables that are repeated measures of a similar value that you want to name consistently.&amp;nbsp; Say you had 6 date variables in a row.&amp;nbsp; You could name them date1,date2, ... date6 and then in code use DATE1-DATE6 to refer to all 6 at once.&lt;/P&gt;
&lt;P&gt;In particular for reading from delimited files I prefer to define the variables first.&amp;nbsp; For example by using the LENGTH statement to tell SAS whether the variable is character or numeric and how many bytes to reserve to store the values.&amp;nbsp; $ indicates a character variable.&amp;nbsp; SAS stores all numbers as 8 byte floating point numbers, so length should be 8.&amp;nbsp; (You can store only part of the 8 byte floating point number, but then you lose precision).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   infile 'myfile' dsd truncover ;
   length 
     Firstvar $20
     nextvar 8
...
     next2lastvar $10
     lastvar $500 
   ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One way to help with that is to just copy the first line from the CSV file and turn it into the LENGTH statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the INPUT statement can use a positional variable list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  input firstvar -- lastvar;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If any of your variable require special informats to be used when reading from the text file or special formats so that their values are clear to humans (this mainly means date, time and datetime values) then add an INFORMAT and/or FORMAT statement to attach the appropriate informat or format to those variables. Most variables do not need either an informat nor a format attached.&amp;nbsp; For example you might have a date string in you file in MDY order, in which case you would use this informat.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  informat mydate mmddyy.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And now that you have a date value you need to use one of the many date type formats so it will display in a human understandable way.&amp;nbsp; I prefer to avoid MDY or DMY order for dates as they are potentially confusing.&amp;nbsp; Is 10/12 the tenth of December or November twelfth?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  format mydate yymmdd10.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could include informat specifications in the INPUT statement.&amp;nbsp; But when reading a delimited file, like a CSV file, make sure to prefix the informat with : so that SAS knows to still read the field in list mode so that it honors the delimiters in the line.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  input ... .mydate :mmddyy. ... ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jul 2020 19:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-multiple-CSV-files-and-create-one-SAS-dataset/m-p/668454#M200378</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-10T19:23:52Z</dc:date>
    </item>
  </channel>
</rss>

