<?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 Import data from several .txt files in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/506462#M15717</link>
    <description>&lt;P&gt;Hello SAS community members,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to import a single row of data from ca. 60 .txt files. I can get it to work for individual files, but when I try to import data from all files in a folder, it does not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My aim is to extract only two pieces of information from each of these files. The start and end clock times of a data recording. These are observations "Start Date &amp;amp; Time" and "End time" in the three attached .txt documents respectively (although I do not reference these variable names in the code). I need the data from row 8 only.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once imported into SAS, I can then extract the clock times from these observations and create new variables using the SCAN function.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS version 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please help me get this to work for all my .txt files in a folder?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ian&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let pathname=C:\;&lt;BR /&gt;Data RES004_AN;&lt;BR /&gt;&amp;nbsp;infile&lt;BR /&gt;"C:\RES004_AN.txt"&lt;BR /&gt;dlm='09' firstobs=8 obs = 8 ;&lt;BR /&gt;&amp;nbsp;input&lt;BR /&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46433"&gt;@01&lt;/a&gt;Var1 1.&lt;BR /&gt;@02 Var2 $20.&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8008"&gt;@22&lt;/a&gt;Var3 $9.&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75109"&gt;@30&lt;/a&gt;Var4 $12.&lt;BR /&gt;@43 Var5 7.;&lt;BR /&gt;&lt;BR /&gt;Num = Var1;&lt;BR /&gt;StarDatTim = Var2;&lt;BR /&gt;EndTim = Var3;&lt;BR /&gt;Dur = Var4;&lt;BR /&gt;DurSec = Var5;&lt;BR /&gt;&lt;BR /&gt;length filename fname $256 ;&lt;BR /&gt;&amp;nbsp; infile "&amp;amp;pathname\*.txt" dsd&amp;nbsp; truncover filename=fname ;&lt;BR /&gt;&amp;nbsp; array d (1000) _temporary_ ;&lt;BR /&gt;&amp;nbsp; length dummy $8 ;&lt;BR /&gt;&amp;nbsp; input @;&lt;BR /&gt;&amp;nbsp; filename=scan(fname,-2,'./\') ;&lt;BR /&gt;&lt;BR /&gt;drop dummy Var1 -- Var5;&lt;BR /&gt;RecStar =scan(StarDatTim,-1,' ');&lt;BR /&gt;RecEnd =scan(EndTim,-1,' ');&lt;BR /&gt;&lt;BR /&gt;;run;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Oct 2018 14:44:40 GMT</pubDate>
    <dc:creator>USCSS_Nostromo</dc:creator>
    <dc:date>2018-10-22T14:44:40Z</dc:date>
    <item>
      <title>Import data from several .txt files</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/506462#M15717</link>
      <description>&lt;P&gt;Hello SAS community members,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to import a single row of data from ca. 60 .txt files. I can get it to work for individual files, but when I try to import data from all files in a folder, it does not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My aim is to extract only two pieces of information from each of these files. The start and end clock times of a data recording. These are observations "Start Date &amp;amp; Time" and "End time" in the three attached .txt documents respectively (although I do not reference these variable names in the code). I need the data from row 8 only.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once imported into SAS, I can then extract the clock times from these observations and create new variables using the SCAN function.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS version 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please help me get this to work for all my .txt files in a folder?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ian&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let pathname=C:\;&lt;BR /&gt;Data RES004_AN;&lt;BR /&gt;&amp;nbsp;infile&lt;BR /&gt;"C:\RES004_AN.txt"&lt;BR /&gt;dlm='09' firstobs=8 obs = 8 ;&lt;BR /&gt;&amp;nbsp;input&lt;BR /&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46433"&gt;@01&lt;/a&gt;Var1 1.&lt;BR /&gt;@02 Var2 $20.&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8008"&gt;@22&lt;/a&gt;Var3 $9.&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75109"&gt;@30&lt;/a&gt;Var4 $12.&lt;BR /&gt;@43 Var5 7.;&lt;BR /&gt;&lt;BR /&gt;Num = Var1;&lt;BR /&gt;StarDatTim = Var2;&lt;BR /&gt;EndTim = Var3;&lt;BR /&gt;Dur = Var4;&lt;BR /&gt;DurSec = Var5;&lt;BR /&gt;&lt;BR /&gt;length filename fname $256 ;&lt;BR /&gt;&amp;nbsp; infile "&amp;amp;pathname\*.txt" dsd&amp;nbsp; truncover filename=fname ;&lt;BR /&gt;&amp;nbsp; array d (1000) _temporary_ ;&lt;BR /&gt;&amp;nbsp; length dummy $8 ;&lt;BR /&gt;&amp;nbsp; input @;&lt;BR /&gt;&amp;nbsp; filename=scan(fname,-2,'./\') ;&lt;BR /&gt;&lt;BR /&gt;drop dummy Var1 -- Var5;&lt;BR /&gt;RecStar =scan(StarDatTim,-1,' ');&lt;BR /&gt;RecEnd =scan(EndTim,-1,' ');&lt;BR /&gt;&lt;BR /&gt;;run;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Oct 2018 14:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/506462#M15717</guid>
      <dc:creator>USCSS_Nostromo</dc:creator>
      <dc:date>2018-10-22T14:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Import data from several .txt files</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/506557#M15726</link>
      <description>&lt;P&gt;From what your post says, you have the actual "get the needed data from a file" part working, so I'm not going to consider that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In terms of getting your data from the different datasets:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. You need to put your successful code into a SAS macro, where the name of the dataset is a parameter, and you have some way of distinguising one output from another (an incremented counter would work fine).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Then you need a way to get the names of all the datasets in a directory. Take a look at the SAS "External Files" routines, they do exactly that. Here's a reference to how to use them:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.mwsug.org/proceedings/2012/S1/MWSUG-2012-S128.pdf" target="_self"&gt;http://www.mwsug.org/proceedings/2012/S1/MWSUG-2012-S128.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Then just get the dataset names, and invoke your macro for each dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Mon, 22 Oct 2018 18:27:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/506557#M15726</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2018-10-22T18:27:48Z</dc:date>
    </item>
    <item>
      <title>Re: Import data from several .txt files</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/507675#M15742</link>
      <description>&lt;P&gt;Hello Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your thoughtful reply to my post. I appreciate these suggestions. My SAS skills are limited, so it will take me some to time to implement everything. I am working on it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ian&lt;/P&gt;</description>
      <pubDate>Fri, 26 Oct 2018 08:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/507675#M15742</guid>
      <dc:creator>USCSS_Nostromo</dc:creator>
      <dc:date>2018-10-26T08:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: Import data from several .txt files</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/508056#M15749</link>
      <description>&lt;P&gt;Neat use of those functions!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it’s supposed in your environment, you can get more detail on the files by streaming the output of a directory listing command into a dataset, so that you can access the files’s timestamps and sizes, and also process into sub directories.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can be useful if you want to process the files in date or size order, or the like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do need to have “xcmds” enabled, which is&amp;nbsp;often not the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a feature where you can process all the files that match a file spec, say c:\Data\Report_*.xls, but that isn’t a great idea in a lot of circumstances, much better to know the names of the files and loop through them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One tip is that you can incorporate the name the files as a field into each row of data, which can be useful for attribution or lineage purposes.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 00:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Import-data-from-several-txt-files/m-p/508056#M15749</guid>
      <dc:creator>AngusLooney</dc:creator>
      <dc:date>2018-10-28T00:18:01Z</dc:date>
    </item>
  </channel>
</rss>

