<?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 csv header information into a macro variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821283#M34896</link>
    <description>Thanks Kurt. Your code makes sense, but it doesn't work.</description>
    <pubDate>Fri, 01 Jul 2022 12:34:46 GMT</pubDate>
    <dc:creator>linlin87</dc:creator>
    <dc:date>2022-07-01T12:34:46Z</dc:date>
    <item>
      <title>Import csv header information into a macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821261#M34893</link>
      <description>&lt;P&gt;Hi SAS Community!&lt;BR /&gt;&lt;BR /&gt;I have managed to use a for loop to import csv files and combine them, but for each csv file there is information in the header that I want to assign as a column for that csv file. For example, I have the following csv file:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs	Date	Time	Growth
System = 2021323 Batch=294 Temp=52 User=012			
%£ KIISGL-20JF FGF2202XA 20J			
1	06/06/2021	12:31:00	34.27327459
2	07/06/2021	12:26:00	35.17687849
3	08/06/2021	12:41:09	62.45345072
4	09/06/2021	12:56:08	29.32030264
5	10/06/2021	13:11:04	26.51833713
6	12/06/2021	13:26:08	34.83632567
7	13/06/2021	13:21:10	13.85211024
8	14/06/2021	13:37:30	42.6087162
8	15/06/2021	13:30:59	66.03200409
9	16/06/2021	13:10:49	95.95908292
10	17/06/2021	13:38:29	91.10805639
11	18/06/2021	13:42:10	55.87138083&lt;/PRE&gt;
&lt;P&gt;There are a number of pieces of information in the header I have been ignoring, but I want to grab. I want the info in the second and third row, particularly the information highlighted in row 3: %£ KIISGL-20JF &lt;FONT color="#FF6600"&gt;FGF2202XA&lt;/FONT&gt; 20J.&lt;BR /&gt;&lt;BR /&gt;So far I have been using infile to import the csv's with firstobs=4, which gets me the data but obviously misses this extra needed information. How do I get the header information (possibly as a separate data step) so that I can add these information to my data files?&lt;BR /&gt;&lt;BR /&gt;I was hoping to assign these bits of the headers as macro variables so I can use them later in my code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help on this would be amazing!&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 08:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821261#M34893</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2022-07-01T08:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: Import csv header information into a macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821274#M34894</link>
      <description>&lt;P&gt;You can do it in one step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile  "have" firstobs=2,
if _n_ = 1 /* second row of infile */
then do;
  call symputx("mvar1",_infile_);
end;
else if _n_ = 2 /* third row */
then do;
  call symputx("mvar2",_infile_);
end;
else do;
  input obs date :ddmmyy10. time :time8. growth;
  output;
end;
format date yymmdd10. time time8.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Jul 2022 11:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821274#M34894</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-07-01T11:56:08Z</dc:date>
    </item>
    <item>
      <title>Re: Import csv header information into a macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821278#M34895</link>
      <description>&lt;P&gt;With out seeing exactly how you want to use that stuff I would suggest not worrying about macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example of reading that information into variables in the data set. Obviously your INFILE would point to your data with appropriate options. You did not describe exactly which pieces you might want.&lt;/P&gt;
&lt;P&gt;This parses you header information and when it finds the line starting with System, the =: if you haven't seen that before is "begins with" comparison, reads the first line into variables , then reads the second line into one variable. You could use a different input statement to read that second bit differently. Other than the first 2 rows the data are read as typical variables. The Output means that records are only written to the output data set after the header rows are processed.&lt;/P&gt;
&lt;P&gt;I retain the header information so that all of the records have that information.&lt;/P&gt;
&lt;P&gt;The @ on the input statement keeps the input pointer on the row. _INFILE_ is the contents of that current line (up to 32K characters) so you can test it for parsing or other actions as desired. You use an Input; to advance to the next line of the file.&lt;/P&gt;
&lt;P&gt;Note that your line with System allows use of one of the uncommon forms of input with the =. There are some limits on this if your other data has values with spaces imbedded.&lt;/P&gt;
&lt;P&gt;You would use Firstobs=2 to start reading at the second line. OR as an exercise left to the interested read add a test for the start of the OBS (assuming that is part of the header) and just don't read that. With that change you could use an Infile similar to : Infile "c:\path\file*.csv" (or what ever common name element and read multiple files in one pass.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assumptions: the System always comes before the other header which always follows on the following line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   infile datalines line=inline;
   informat system $10. batch 5. temp 2. User $4.   header2 $40.
           Obs f4.	Date ddmmyy10.	Time time8.	Growth 12.
           ;
   format date ddmmyy10. time time.;
   retain system batch temp user header2;
   input @;
   if _infile_ =: 'System' then do;
     input system= Batch= Temp= user=;
     input @;
     header2=_infile_;
     input;
   end;
   else do;
      input obs date time growth;
      output;
   end;

datalines;
System = 2021323 Batch=294 Temp=52 User=012			
%£ KIISGL-20JF FGF2202XA 20J			
1	06/06/2021	12:31:00	34.27327459
2	07/06/2021	12:26:00	35.17687849
3	08/06/2021	12:41:09	62.45345072
4	09/06/2021	12:56:08	29.32030264
5	10/06/2021	13:11:04	26.51833713
6	12/06/2021	13:26:08	34.83632567
7	13/06/2021	13:21:10	13.85211024
;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS Community!&lt;BR /&gt;&lt;BR /&gt;I have managed to use a for loop to import csv files and combine them, but for each csv file there is information in the header that I want to assign as a column for that csv file. For example, I have the following csv file:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs	Date	Time	Growth
System = 2021323 Batch=294 Temp=52 User=012			
%£ KIISGL-20JF FGF2202XA 20J			
1	06/06/2021	12:31:00	34.27327459
2	07/06/2021	12:26:00	35.17687849
3	08/06/2021	12:41:09	62.45345072
4	09/06/2021	12:56:08	29.32030264
5	10/06/2021	13:11:04	26.51833713
6	12/06/2021	13:26:08	34.83632567
7	13/06/2021	13:21:10	13.85211024
8	14/06/2021	13:37:30	42.6087162
8	15/06/2021	13:30:59	66.03200409
9	16/06/2021	13:10:49	95.95908292
10	17/06/2021	13:38:29	91.10805639
11	18/06/2021	13:42:10	55.87138083&lt;/PRE&gt;
&lt;P&gt;There are a number of pieces of information in the header I have been ignoring, but I want to grab. I want the info in the second and third row, particularly the information highlighted in row 3: %£ KIISGL-20JF &lt;FONT color="#FF6600"&gt;FGF2202XA&lt;/FONT&gt; 20J.&lt;BR /&gt;&lt;BR /&gt;So far I have been using infile to import the csv's with firstobs=4, which gets me the data but obviously misses this extra needed information. How do I get the header information (possibly as a separate data step) so that I can add these information to my data files?&lt;BR /&gt;&lt;BR /&gt;I was hoping to assign these bits of the headers as macro variables so I can use them later in my code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help on this would be amazing!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 12:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821278#M34895</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-07-01T12:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Import csv header information into a macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821283#M34896</link>
      <description>Thanks Kurt. Your code makes sense, but it doesn't work.</description>
      <pubDate>Fri, 01 Jul 2022 12:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821283#M34896</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2022-07-01T12:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Import csv header information into a macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821288#M34897</link>
      <description>&lt;P&gt;Why would you use PROC IMPORT to read a text file with only four variables?&lt;/P&gt;
&lt;P&gt;What you posted is not a CSV file.&amp;nbsp; It appears to have TAB characters separating the values, not COMMA that is used a Comma Separated Values file.&amp;nbsp; Does the original file use commas or tabs? Or some other delimiter character?&amp;nbsp; Is it possible you have accidentally opened the CSV file with EXCEL and are showing the result of EXCEL reading the CSV file?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway assuming have a CSV file you could read that into a dataset with code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile cards dsd truncover firstobs=3 ;
  if _n_=1 then do ; 
     input row1 :$50.;
     code = scan(row1,3,' ');
     retain row1 code;
  end;
  input obs date :ddmmyy. time :time. growth;
  format date yymmdd10. time tod8. ;
cards4;
Obs,Date,Time,Growth
System = 2021323 Batch=294 Temp=52 User=012,,,
%£ KIISGL-20JF FGF2202XA 20J,,,
1,06/06/2021,12:31:00,34.27327459
2,07/06/2021,12:26:00,35.17687849
3,08/06/2021,12:41:09,62.45345072
4,09/06/2021,12:56:08,29.32030264
5,10/06/2021,13:11:04,26.51833713
6,12/06/2021,13:26:08,34.83632567
7,13/06/2021,13:21:10,13.85211024
8,14/06/2021,13:37:30,42.6087162
8,15/06/2021,13:30:59,66.03200409
9,16/06/2021,13:10:49,95.95908292
10,17/06/2021,13:38:29,91.10805639
11,18/06/2021,13:42:10,55.87138083
;;;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1656680049136.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72892i9E4AFA68A4030C1C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1656680049136.png" alt="Tom_0-1656680049136.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;And if you have multiple CSV files in the same structure you could read them all into a single SAS dataset using a single data step also.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 12:59:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/821288#M34897</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-01T12:59:11Z</dc:date>
    </item>
    <item>
      <title>Re: Import csv header information into a macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/822277#M34971</link>
      <description>&lt;P&gt;Hi Tom,&lt;BR /&gt;&lt;BR /&gt;Yes I opened it in excel and that is why it is showing as tab delimited.&lt;BR /&gt;&lt;BR /&gt;I now have the following code for getting the directories of each .csv file:&lt;/P&gt;
&lt;P&gt;filename tmp pipe 'dir "Z:\JLB_Study\Data\*hamrit.csv" /b /s';&lt;BR /&gt;data direct;&lt;BR /&gt;infile tmp dlm="¬";&lt;BR /&gt;length cmd_line $200;&lt;BR /&gt;input cmd_line;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;My question is what is this "¬" as a delimiter? I inherited this code, and I can't find any documentation on this. I know it is the logical symbol for NOT but I don't know why this would be used for a delimiter for csv files. I assumed it would instead by ",".&lt;BR /&gt;&lt;BR /&gt;Any help on what this is would be great!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jul 2022 13:56:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/822277#M34971</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2022-07-08T13:56:47Z</dc:date>
    </item>
    <item>
      <title>Re: Import csv header information into a macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/822279#M34972</link>
      <description>&lt;P&gt;Probably a trick to try to read the full line.&amp;nbsp; So if that strange character never appears on the line then the INPUT statement will read the whole line (or at least the first 200 bytes) in the variable CMD_LINE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Probably would be easier to use the TRUNCOVER option instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename tmp pipe 'dir "Z:\JLB_Study\Data\*hamrit.csv" /b /s';
data direct;
  infile tmp truncover;
  input cmd_line $200.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can also eliminate the FILENAME statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data direct;
  infile 'dir "Z:\JLB_Study\Data\*hamrit.csv" /b /s' pipe truncover;
  input cmd_line $200.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if the goal is to just read all of the CSV files you can skip making the list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile "Z:\JLB_Study\Data\*hamrit.csv" dsd filename=fname ;
  input @;
  file = scan(fname,-1,'/\');
  if file ne lag(file) then do;
     input // row1 :$50.;
     code = scan(row1,3,' ');
     retain row1 code;
  end;
  input obs date :ddmmyy. time :time. growth;
  format date yymmdd10. time tod8. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Jul 2022 14:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/822279#M34972</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-08T14:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: Import csv header information into a macro variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/822816#M35034</link>
      <description>Thanks for that Tom. It is interesting and I'll consider that for other applications, but I actually need to make data direct because in each folder, I also need to grab some other .csv files with slightly different suffixes (not *hamrit.csv). If you had a better way of doing this I'd be keen to hear!</description>
      <pubDate>Tue, 12 Jul 2022 12:37:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Import-csv-header-information-into-a-macro-variable/m-p/822816#M35034</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2022-07-12T12:37:03Z</dc:date>
    </item>
  </channel>
</rss>

