<?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 read multiple lines of data in a single variable. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334137#M75434</link>
    <description>&lt;P&gt;This do loop works as follows:&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;  do i=1 by 1 while (countc(outtxt,'~')&amp;lt;25);
    input;
    outtxt=catx(' ',outtxt,_infile_);
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The "do i=1 by 1 while (countc(outtxt,'~')&amp;lt;25 says to start doing&amp;nbsp;doing something and keep doing the same thing (i.e. looping) as long as there less than 25 '~' in the outtxt variable. And what is the "thing" being repeatedly done?&lt;BR /&gt;&lt;BR /&gt;First,&amp;nbsp;since every time the top of the data step starts, outtxt is reset to missing, it starts out with 0 '~'.&amp;nbsp; So the do-loop is going to be iterated as least once.&amp;nbsp; Inside the loop it does the following:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; - INPUT;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* reads a line, but populates no variable except&amp;nbsp;automatic variable _INFILE_ which gets a direct copy of the input line.&lt;BR /&gt;&amp;nbsp;&amp;nbsp; - CATX:&amp;nbsp; Concatenates the content of OUTTXT (which starts out blank) with the contents of _INFILE_.&amp;nbsp;&amp;nbsp;For the first&amp;nbsp;iteration, it's just copying _INFILE_ to OUTTXT.&amp;nbsp; For all&amp;nbsp;later iterations, it's &lt;EM&gt;&lt;STRONG&gt;appending&lt;/STRONG&gt;&lt;/EM&gt; &amp;nbsp;_INFILE_ to the non-empty OUTTXT, separating them by a blank)&lt;BR /&gt;&lt;BR /&gt;So this loop keeps extending the content of OUTTXT&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; - go back to top of the loop and rechecks the count of '~' in outtxt.&amp;nbsp; Once it stops being &amp;lt;25 the looping stops and the subsequent statements are executed:&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;After the loop, the contents of OUTTXT is written to the target of the PUT statement.&amp;nbsp; That target is the destination identified by the FILE statement.&lt;/LI&gt;
&lt;/OL&gt;</description>
    <pubDate>Sat, 18 Feb 2017 22:38:21 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-02-18T22:38:21Z</dc:date>
    <item>
      <title>How to read multiple lines of data in a single variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334085#M75398</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to read a file which hasa variable at the end of the record. This variable's value may be in the same record or may go upto multiple line as well. I tried many options but not able to read.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Attached is the sample data and variable names.&lt;/P&gt;&lt;P&gt;Kindly help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Lokesh&lt;/P&gt;</description>
      <pubDate>Sat, 18 Feb 2017 15:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334085#M75398</guid>
      <dc:creator>e044800</dc:creator>
      <dc:date>2017-02-18T15:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to read multiple lines of data in a single variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334100#M75408</link>
      <description>&lt;P&gt;I'm not aware of a way to make proc import read multiple lines as a&amp;nbsp;single observation.&amp;nbsp; So what you want do is make the multiple lines into single lines, when needed - then run proc import.&amp;nbsp; For your '~' separated text file, assume you know in advance that you have 26 data fields (i.e. 25 '~') per record, and that no complete record needs more than 500 characters:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename tmp temp;
data _null_;
  infile 'c:\temp\t.tilde';
  length outtxt $500;
  do i=1 by 1 while (countc(outtxt,'~')&amp;lt;25);
    input;
    outtxt=catx(' ',outtxt,_infile_);
  end;
  file tmp;
  put outtxt;
run;

proc import datafile=tmp
  out=want   dbms=dlm   replace;
  delimiter='~';
  getnames=yes;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Then "filename tmp temp" uses the TEMP location, which means that SAS will find a physical location for the data it will receive, and will also delete the file at the end of your sas session.&lt;/LI&gt;
&lt;LI&gt;OUTTXT is going to contain 1 (or more) concatenated input records until 25 '~' are held.&lt;/LI&gt;
&lt;LI&gt;When an 'input' statement is executed, the automatic variable _INFILE_ contains the input line, regardless of whether the input statement names any variables.&lt;/LI&gt;
&lt;LI&gt;COUNTC function count the number of&amp;nbsp;occurrence of the second argument ('~') that are found in the first argument&lt;/LI&gt;
&lt;LI&gt;CATX concatenate the 2nd and 3rd (and 4th, 5th, ...) arguments separated by the first argument.&lt;/LI&gt;
&lt;LI&gt;FILE tmp provide a destination for the PUT statement.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you tried the proc import against the original file, you'd have problems with multiline records.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Feb 2017 16:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334100#M75408</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-18T16:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to read multiple lines of data in a single variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334109#M75415</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It given me a correct output however i am having little hard time in understanding the do loop logic in your code. Can you please explain it in detail for my understanding.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Like how this is working with _infile_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 18 Feb 2017 18:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334109#M75415</guid>
      <dc:creator>e044800</dc:creator>
      <dc:date>2017-02-18T18:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to read multiple lines of data in a single variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334110#M75416</link>
      <description>&lt;P&gt;Here is a tested code, less elegant than&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;but it gives the desired result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename text_in '/folders/myshortcuts/My_Folders/flat/sample_Survey.txt';

data text;
   retain confirmation_number first_name last_name 
          exam_code form_code site_code testdate
          v1-v9 survey01-survey09 obs;
   length survey09 $200;
   format testdate date9.;
            
   infile text_in truncover firstobs=2 dlm='~' dsd eof=done;
   input confirmation $16. @;  
   obs+1; 
   if input(confirmation,?? 16.) &amp;gt; 0 then do;
       if obs &amp;gt; 1 then output;
   
       input @1 confirmation_number first_name $ last_name $
             exam_code $ form_code $ site_code $ testdate mmddyy10. @+1
             v1 $ v2 $ v3 $ v4 $ v5 $ v6 $ v7 $ v8 $ v9 $ 
             survey01 $ survey02 $ survey03 $ survey04 $ survey05 $
             survey06 $ survey07 $ survey08 $ survey09 $
       ; 
   end; 
   else do;
       input @1 a_line $200.;
       survey09 = catx(' ',survey09,scan(a_line,1,'~'));   
   end;
   drop obs confirmation a_line;
return;
done:
   output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Feb 2017 18:23:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334110#M75416</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-18T18:23:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to read multiple lines of data in a single variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334111#M75417</link>
      <description>I assumed that the extra lines are always the continuation of survey09.</description>
      <pubDate>Sat, 18 Feb 2017 18:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334111#M75417</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-18T18:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to read multiple lines of data in a single variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334137#M75434</link>
      <description>&lt;P&gt;This do loop works as follows:&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;  do i=1 by 1 while (countc(outtxt,'~')&amp;lt;25);
    input;
    outtxt=catx(' ',outtxt,_infile_);
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The "do i=1 by 1 while (countc(outtxt,'~')&amp;lt;25 says to start doing&amp;nbsp;doing something and keep doing the same thing (i.e. looping) as long as there less than 25 '~' in the outtxt variable. And what is the "thing" being repeatedly done?&lt;BR /&gt;&lt;BR /&gt;First,&amp;nbsp;since every time the top of the data step starts, outtxt is reset to missing, it starts out with 0 '~'.&amp;nbsp; So the do-loop is going to be iterated as least once.&amp;nbsp; Inside the loop it does the following:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; - INPUT;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* reads a line, but populates no variable except&amp;nbsp;automatic variable _INFILE_ which gets a direct copy of the input line.&lt;BR /&gt;&amp;nbsp;&amp;nbsp; - CATX:&amp;nbsp; Concatenates the content of OUTTXT (which starts out blank) with the contents of _INFILE_.&amp;nbsp;&amp;nbsp;For the first&amp;nbsp;iteration, it's just copying _INFILE_ to OUTTXT.&amp;nbsp; For all&amp;nbsp;later iterations, it's &lt;EM&gt;&lt;STRONG&gt;appending&lt;/STRONG&gt;&lt;/EM&gt; &amp;nbsp;_INFILE_ to the non-empty OUTTXT, separating them by a blank)&lt;BR /&gt;&lt;BR /&gt;So this loop keeps extending the content of OUTTXT&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; - go back to top of the loop and rechecks the count of '~' in outtxt.&amp;nbsp; Once it stops being &amp;lt;25 the looping stops and the subsequent statements are executed:&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;After the loop, the contents of OUTTXT is written to the target of the PUT statement.&amp;nbsp; That target is the destination identified by the FILE statement.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sat, 18 Feb 2017 22:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-multiple-lines-of-data-in-a-single-variable/m-p/334137#M75434</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-18T22:38:21Z</dc:date>
    </item>
  </channel>
</rss>

