<?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 Parsing a file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294104#M270185</link>
    <description>&lt;P&gt;I'm trying to parse an input dataset, to output only certain lines and can't get that working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My input files looks like this –&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Top&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;/LOGON&lt;/P&gt;&lt;P&gt;Line 1&lt;/P&gt;&lt;P&gt;Line 2&lt;/P&gt;&lt;P&gt;Etc&lt;/P&gt;&lt;P&gt;/LOGOFF&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;Bottom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write the lines found between the /LOGON and /LOGOFF out to a dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been playing around with variations of this, but its not giving me what I'm after –&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA COMMAND;                                  
     INFILE FLAGFILE;                          
     INPUT @2  CMD     $50.;                   
                                               
  IF SUBSTR(CMD,2,6) = '/LOGON' then           
    do while (SUBSTR(CMD,2,7) ¬= '/LOGOFF');   
       put CMD;                                
    end;                                       &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks….&lt;/P&gt;</description>
    <pubDate>Thu, 25 Aug 2016 17:24:44 GMT</pubDate>
    <dc:creator>serge68</dc:creator>
    <dc:date>2016-08-25T17:24:44Z</dc:date>
    <item>
      <title>Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294104#M270185</link>
      <description>&lt;P&gt;I'm trying to parse an input dataset, to output only certain lines and can't get that working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My input files looks like this –&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Top&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;/LOGON&lt;/P&gt;&lt;P&gt;Line 1&lt;/P&gt;&lt;P&gt;Line 2&lt;/P&gt;&lt;P&gt;Etc&lt;/P&gt;&lt;P&gt;/LOGOFF&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;Bottom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write the lines found between the /LOGON and /LOGOFF out to a dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been playing around with variations of this, but its not giving me what I'm after –&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA COMMAND;                                  
     INFILE FLAGFILE;                          
     INPUT @2  CMD     $50.;                   
                                               
  IF SUBSTR(CMD,2,6) = '/LOGON' then           
    do while (SUBSTR(CMD,2,7) ¬= '/LOGOFF');   
       put CMD;                                
    end;                                       &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks….&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 17:24:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294104#M270185</guid>
      <dc:creator>serge68</dc:creator>
      <dc:date>2016-08-25T17:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294120#M270186</link>
      <description>&lt;P&gt;Does this help:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   input @;
   length cmd $ 50.;
   if index (_infile_,"/LOGON") &amp;gt; 0 then input;
   else if index (_infile_,"/LOGOFF")&amp;gt;0 then input;
   else do;
      cmd=_infile_;
      output;
   end;
datalines;
/LOGON
Line 1
Line 2
Etc
/LOGOFF
/LOGON
second Line 1
second Line 2
second Etc
second etc 2
/LOGOFF
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2016 18:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294120#M270186</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-08-25T18:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294126#M270187</link>
      <description>&lt;P&gt;The problem needs a little clarification.&amp;nbsp; Are there lines before the first /LOGON that you need to ignore?&amp;nbsp; Are there lines after the final /LOGOFF that you also need to ignore?&amp;nbsp; Could there be multiple pairs of logons and logoffs, with garbage in between?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program might be as simple as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data command;&lt;/P&gt;
&lt;P&gt;input flagfile truncover;&lt;/P&gt;
&lt;P&gt;input @2 cmd $50.;&lt;/P&gt;
&lt;P&gt;if cmd in ('/LOGON', '/LOGOFF') then delete;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 18:15:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294126#M270187</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-25T18:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294136#M270188</link>
      <description>&lt;P&gt;Anticipating the answers to some of the questions, this would be a flexible way to approach the problem:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data command;&lt;/P&gt;
&lt;P&gt;infile flagfile truncover;&lt;/P&gt;
&lt;P&gt;retain status 'logged out';&lt;/P&gt;
&lt;P&gt;input @2&amp;nbsp; cmd $50.;&lt;/P&gt;
&lt;P&gt;if cmd='/LOGON' then status='logged in';&lt;/P&gt;
&lt;P&gt;else if cmd='/LOGOFF' then status='logged out';&lt;/P&gt;
&lt;P&gt;else if status='logged in' then output;&lt;/P&gt;
&lt;P&gt;drop status;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not clear whether a function (substr, index) needs to be applied when searching for /LOGON or /LOGOFF ... depends on what is actually in your data lines.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 18:41:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294136#M270188</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-25T18:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294137#M270189</link>
      <description>&lt;P&gt;You have two types of lies: lines to skip and lines to output.&lt;/P&gt;&lt;P&gt;in such case I'll do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data out;&lt;/P&gt;&lt;P&gt;&amp;nbsp; retain phase 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; infile ... truncover;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input a_line $80.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if index(a_line, '/LOGON' &amp;nbsp;) &amp;gt; 0 then do; phase=1; &amp;nbsp;input a_line; end; &amp;nbsp; /* skipping the /LOGON line */&lt;/P&gt;&lt;P&gt;&amp;nbsp; if index(a_line, '/LOGOFF' ) &amp;gt; 0 then phase = 0; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* skipping the /LOGOFF line and the follows */&lt;/P&gt;&lt;P&gt;&amp;nbsp; if phase=1 then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; drop phase;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please check does it fits your request.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 18:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294137#M270189</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-08-25T18:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294155#M270190</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; – not quite. This includes data before and after the ?LOGON &amp;amp; ?LOGOFF that I want to drop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; – yes there are lines before ?LOGON that I need to ignore. And lines after /LOGOFF I need to ignore. There is a single pair og LOGON &amp;amp; LOGOFF, and it’s the data between them which is valid.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;@Shmuel&amp;nbsp; - this drops all lines preceding and including the /Logon, it out puts the data I want. However it then outputs all data after the /LOGOFF which I need to drop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks all.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 19:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294155#M270190</guid>
      <dc:creator>serge68</dc:creator>
      <dc:date>2016-08-25T19:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294163#M270191</link>
      <description>&lt;P&gt;when /LOGOFF is found the PHASE is assigned to 0, so no output will be done on next lines&lt;/P&gt;&lt;P&gt;except if a new /LOGON line encountered.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 19:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294163#M270191</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-08-25T19:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294169#M270192</link>
      <description>&lt;P&gt;Based on your last explanation, here is a shortcut that would improve the speed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data command;&lt;/P&gt;
&lt;P&gt;infile flagfile truncover;&lt;/P&gt;
&lt;P&gt;retain status 'logged out';&lt;/P&gt;
&lt;P&gt;input @2&amp;nbsp; cmd $50.;&lt;/P&gt;
&lt;P&gt;if cmd='/LOGON' then status='logged in';&lt;/P&gt;
&lt;P&gt;else if cmd='/LOGOFF' then stop;&lt;/P&gt;
&lt;P&gt;else if status='logged in' then output;&lt;/P&gt;
&lt;P&gt;drop status;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Again, this relies on cmd being exactly "/LOGON" or "/LOGOFF".&amp;nbsp; If there are other characters on the line (including leading blanks), another function might have to be applied.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 19:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294169#M270192</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-25T19:51:40Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294172#M270193</link>
      <description>&lt;P&gt;Hi Schmuel - You are correct, the issue is I have a second LOGON &amp;amp; LOGON, though not in columns 2-8. Thats why I had attempted the SUBSTR earlier, as its only the LOGON/LOGOFF in cols 2-8 that I'm concerned with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 20:00:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294172#M270193</guid>
      <dc:creator>serge68</dc:creator>
      <dc:date>2016-08-25T20:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294178#M270194</link>
      <description>&lt;P&gt;Then just change: if index(a_line, ...) = 2 instead if ... &amp;gt; 0&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 20:08:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294178#M270194</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-08-25T20:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294180#M270195</link>
      <description>&lt;P&gt;If there may be only one pair of /LOGIN - /LOGOFF then&lt;/P&gt;&lt;P&gt;you better do:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if index(a_line, '/LOGOFF') = 2 then stop;&lt;/P&gt;&lt;P&gt;No need to continue reading the file.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 20:11:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294180#M270195</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-08-25T20:11:18Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294183#M270196</link>
      <description>&lt;P&gt;Needed to go =1 vs =2, but that gives me what I'm after.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate the assistance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 25 Aug 2016 20:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294183#M270196</guid>
      <dc:creator>serge68</dc:creator>
      <dc:date>2016-08-25T20:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing a file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294192#M270197</link>
      <description>&lt;P&gt;You need a RETAINed variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA COMMAND;                                  
retain keep;
INFILE datalines;                          
INPUT CMD $50.; 
if left(_infile_) = "/LOGON" then keep=1;
else if left(_infile_) = "/LOGOFF" then keep=0;
else if keep then output;
drop keep;
datalines;
Before
/LOGON
Line 1
Line 2
Etc
/LOGOFF
After
;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2016 20:54:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Parsing-a-file/m-p/294192#M270197</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-08-25T20:54:36Z</dc:date>
    </item>
  </channel>
</rss>

