<?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: Reading a txt file to create macro variables. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734733#M228874</link>
    <description>&lt;P&gt;You can save half the work by using ODS.&amp;nbsp; Whatever you see as a header file can be captured as a SAS data set using ODS.&amp;nbsp; You would need to run the PROC after adding:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ODS TRACE ON;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That will reveal the names of the outputs that are available as data sets.&amp;nbsp; Once you know the names, you can add an ODS SELECT statement to capture that as a SAS data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you know how to take a variable in a SAS data set and transfer it to a macro variable?&lt;/P&gt;</description>
    <pubDate>Fri, 16 Apr 2021 14:31:34 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2021-04-16T14:31:34Z</dc:date>
    <item>
      <title>Reading a txt file to create macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734715#M228870</link>
      <description>&lt;P&gt;I have txt response header file created out of proc http . i need to read in status code and a url from the response file to create global macrovaribales.&lt;/P&gt;&lt;P&gt;Here is my txt file:&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;HTTP/1.1 202 Accepted&lt;BR /&gt;Content-Length: 0&lt;BR /&gt;Cache-Control: no-cache; no-store; must-revalidate; max-age=0&lt;BR /&gt;Content-Location: &lt;A href="https://testapi.gov.in/api/v1/jobs/4648" target="_blank"&gt;https://testapi.gov.in/api/v1/jobs/4648&lt;/A&gt;&lt;BR /&gt;Pragma: no-cache&lt;BR /&gt;Date: Fri, 16 Apr 2021 10:58:50 GMT&lt;BR /&gt;Connection: keep-alive&lt;BR /&gt;Strict-Transport-Security: max-age=86400&lt;BR /&gt;X-Content-Type-Options: nosniff&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I need to create macro variable of the status code from 1st line and URL from the 4th line.&lt;/P&gt;&lt;P&gt;Till now i was just reading in the code using this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename header "/test/api/exec_job_head.txt";&lt;BR /&gt;data test;
   infile header dlm='09'x;
   length header_col $100.;
   input header_col ;
   
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Apr 2021 13:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734715#M228870</guid>
      <dc:creator>khandelwalanmol</dc:creator>
      <dc:date>2021-04-16T13:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a txt file to create macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734733#M228874</link>
      <description>&lt;P&gt;You can save half the work by using ODS.&amp;nbsp; Whatever you see as a header file can be captured as a SAS data set using ODS.&amp;nbsp; You would need to run the PROC after adding:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ODS TRACE ON;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That will reveal the names of the outputs that are available as data sets.&amp;nbsp; Once you know the names, you can add an ODS SELECT statement to capture that as a SAS data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you know how to take a variable in a SAS data set and transfer it to a macro variable?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Apr 2021 14:31:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734733#M228874</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-04-16T14:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a txt file to create macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734779#M228890</link>
      <description>&lt;P&gt;The status code will always be in the first line, but the url might possibly be in lines different than the 4th.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use SCAN function to extract relevant portions and SYMPUT to move the value from the DATA Step to the macro environment.&amp;nbsp; Because the operation is for only populating macro variables no output data set need be created, hence DATA _NULL_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;filename header "/test/api/exec_job_head.txt";
data _null_;
   infile header;
   input;  /* fill automatic _infile_ variable with entire line */
   if _n_ = 1 then do;
     call symput ('status', scan(_infile_,2,'0920'x)); /* extract 2nd item allowing for both tab and space optional whitespace */
   end;
   else
   if upcase(_infile_) = 'CONTENT-LOCATION:' then do;
     place = index (_infile_, ':');
     call symput('URL', strip(substr(_infile_, place+1)));
     stop;
   end;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Apr 2021 17:29:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734779#M228890</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2021-04-16T17:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: Reading a txt file to create macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734790#M228894</link>
      <description>&lt;P&gt;Why did you use tab as the delimiter on the INFILE statement?&amp;nbsp; Are you sure the file has tabs (or are you sure the file does NOT have tabs)?&lt;/P&gt;
&lt;P&gt;Also are you sure the file will have line breaks?&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;Many "response" files are sent without any line breaks.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Let's convert your posting into an actual file:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options parmcards=header ;
filename header temp;

parmcards4;
HTTP/1.1 202 Accepted
Content-Length: 0
Cache-Control: no-cache; no-store; must-revalidate; max-age=0
Content-Location: https://testapi.gov.in/api/v1/jobs/4648
Pragma: no-cache
Date: Fri, 16 Apr 2021 10:58:50 GMT
Connection: keep-alive
Strict-Transport-Security: max-age=86400
X-Content-Type-Options: nosniff
;;;;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please show what output you expect to get from that example input.&amp;nbsp; What part of that is the STATUS? The URL?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To be more robust you probably want to test what the line LOOKS like rather than its position.&lt;/P&gt;
&lt;P&gt;Assuming the file actually has line breaks you might use something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do row=1 by 1 until (eof);
    infile header truncover end=eof;
    input @;
    if row=1 then input status $100. ;
    else if _infile_=:'Content-Location:' then input @':' url $200. ;
    else input;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    row           status                              url

 1      9     HTTP/1.1 202 Accepted    https://testapi.gov.in/api/v1/jobs/4648
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It might just be easier to read the file as NAME/VALUE pairs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data headers ;
  infile header dlm=':' truncover ;
  length field $100 value $200 ;
  if _n_=1 then do;
    field='Status'; input value $200.;
  end;
  else input field value $200.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    field                        value

 1     Status                       HTTP/1.1 202 Accepted
 2     Content-Length               0
 3     Cache-Control                no-cache; no-store; must-revalidate; max-age=0
 4     Content-Location             https://testapi.gov.in/api/v1/jobs/4648
 5     Pragma                       no-cache
 6     Date                         Fri, 16 Apr 2021 10:58:50 GMT
 7     Connection                   keep-alive
 8     Strict-Transport-Security    max-age=86400
 9     X-Content-Type-Options       nosniff
&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Apr 2021 19:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-a-txt-file-to-create-macro-variables/m-p/734790#M228894</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-16T19:45:58Z</dc:date>
    </item>
  </channel>
</rss>

