<?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: how to import mutiple CSV files with different datarows into SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413556#M279944</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Setting the informat sets the length of the variables so you should set reasonable values for your data.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Actually setting the INFORMAT tells SAS how to convert the text it reads into the value that it stores. But SAS already knows how to read character variables so adding $xx INFORMAT for a character variable does not really help SAS in any way.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It does have the SIDE EFFECT of defining the variables as being character variables with the lengths specified in the informat since the INFORMAT statement is the first place that you referenced the variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Why not just DEFINE the variables?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length itemtype itemid author $25;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Nov 2017 03:29:59 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-11-15T03:29:59Z</dc:date>
    <item>
      <title>how to import mutiple CSV files with different datarows into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413409#M279939</link>
      <description>&lt;P&gt;Hello SAS guys,&lt;/P&gt;&lt;P&gt;I need to import mutiple CSV files into SAS. Each files has same formats, but the beginning datarow is different.&lt;/P&gt;&lt;P&gt;For example, the data row begins at line 15 in test1, while the data row begins at line 13 in test2.&lt;/P&gt;&lt;P&gt;My code is the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro readfile;&lt;/P&gt;&lt;P&gt;%do i=1 %to 2;&lt;/P&gt;&lt;P&gt;data want&amp;amp;i. ;&lt;BR /&gt;infile "test&amp;amp;i." dlm='|' dsd lrecl=32767 firstobs=&amp;amp;first.;&lt;BR /&gt;input @;&lt;BR /&gt;itemid=dequote(scan(_INFILE_,2,"|"));&lt;BR /&gt;itemtype=dequote(scan(_INFILE_,1,"|",'M'));&lt;BR /&gt;author=dequote(scan(_INFILE_,3,"|"));&lt;BR /&gt;put _all_;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend readfile;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I get the firstobs=13/15&amp;nbsp; automatically instead of hardcoding 13 or 15.&amp;nbsp; Thanks&amp;nbsp;very much for helping!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Nov 2017 17:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413409#M279939</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2017-11-14T17:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to import mutiple CSV files with different datarows into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413496#M279940</link>
      <description>&lt;P&gt;Read it all and if it's not relevant delete it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or read each individually and append after?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or is there some systematic order of where the datarow starts that can be automated?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Nov 2017 22:01:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413496#M279940</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-14T22:01:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to import mutiple CSV files with different datarows into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413515#M279941</link>
      <description>&lt;P&gt;No, I need to read each file in every loop. Do not need to append them together.&amp;nbsp; Thanks for reply!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Nov 2017 22:45:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413515#M279941</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2017-11-14T22:45:06Z</dc:date>
    </item>
    <item>
      <title>Re: how to import mutiple CSV files with different datarows into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413519#M279942</link>
      <description>&lt;P&gt;Please take a look at this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want&amp;amp;i. ;
   infile "test&amp;amp;i." dlm='|' dsd lrecl=32767 truncover end= dataend;
   informat itemtype itemid author $25.;
   /* reads the lines untile the header "Item Type"| appears*/
   do until (scan(_infile_,1,'|"') in ('Item Type') );
      INPUT ;
   END;
   /* now read the data*/
   do while (dataend=0);
      input itemtype itemid author ;
      output;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;Instead of trying to find firstobs and the retroactively setting the firstobs infile option the above reads the code until it finds a key value that says that you are reading the header row. Then reads the values from there until the end of the file. If you have blank lines at the end of the file you'll have missing data. The infile option END sets flag that is set to one when the end of the file is encountered. So we use that to control a do loop to execute inputs until the end is encountered. The Explicit output is needed to output each record as read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice that when you use delimited data the quotes in the data file are read between for the text so the dequote and scanning aren't needed. Setting the informat sets the length of the variables so you should set reasonable values for your data.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Nov 2017 23:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413519#M279942</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-11-14T23:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: how to import mutiple CSV files with different datarows into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413520#M279943</link>
      <description>&lt;P&gt;The first row of the data you want is different but there is data before. I see two options, one is to pre-process each file and determine the start row and pass that to your macro. A second is to use INPUT to find the ITEM TYPE line and then set a flag. If that flag is set, then you read the data. I think this works but haven't tested it, so if you do run into issues post your code, log and details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain first_flag;&lt;BR /&gt;input @@;
&lt;BR /&gt;if first_flag=1 then do;&lt;BR /&gt;input ... ;&lt;BR /&gt;end;&lt;BR /&gt;
if _infile_ in: ('"ITEM TYPE"') then first_flag=1;&lt;BR /&gt;&lt;BR /&gt;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Nov 2017 23:19:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413520#M279943</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-14T23:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: how to import mutiple CSV files with different datarows into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413556#M279944</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Setting the informat sets the length of the variables so you should set reasonable values for your data.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Actually setting the INFORMAT tells SAS how to convert the text it reads into the value that it stores. But SAS already knows how to read character variables so adding $xx INFORMAT for a character variable does not really help SAS in any way.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It does have the SIDE EFFECT of defining the variables as being character variables with the lengths specified in the informat since the INFORMAT statement is the first place that you referenced the variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Why not just DEFINE the variables?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length itemtype itemid author $25;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2017 03:29:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413556#M279944</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-15T03:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: how to import mutiple CSV files with different datarows into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413557#M279945</link>
      <description>&lt;P&gt;Just find the starting point yourself.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want&amp;amp;i ;
 length Item_Type $20 Item_ID $20 Author $20 CorrectResponse DQ 8 DQ_Type $20 Folder_Name1 Folder_Name2 Item_Type2 $20 ;
 infile "text&amp;amp;i" dsd dlm='|' truncover ; 
 if _n_=1 then do until(Item_Type='Item Type'); 
    input Item_Type / @;
 end;
 input Item_Type -- Item_Type2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Nov 2017 03:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413557#M279945</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-15T03:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to import mutiple CSV files with different datarows into SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413671#M279946</link>
      <description>&lt;P&gt;Thank you Tom and Ballardw. I tried these two codes and both of them works. Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2017 14:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-import-mutiple-CSV-files-with-different-datarows-into-SAS/m-p/413671#M279946</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2017-11-15T14:57:50Z</dc:date>
    </item>
  </channel>
</rss>

