<?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 can I read unorganized text file to SAS dataset with selected variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922724#M363339</link>
    <description>&lt;P&gt;Thank you for all your help. There is a problem with the consistency in variable cases. For example, I need to have a variable "explanation" to be included but some of them are "Explantation".&lt;/P&gt;</description>
    <pubDate>Wed, 03 Apr 2024 14:04:38 GMT</pubDate>
    <dc:creator>CHL0320</dc:creator>
    <dc:date>2024-04-03T14:04:38Z</dc:date>
    <item>
      <title>How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922619#M363297</link>
      <description>&lt;P&gt;Dear Community;&lt;/P&gt;
&lt;P&gt;I need to read a text file that is a codebook to create an SAS dataset. This SAS dataset only contains 4 variables,&amp;nbsp;var_name, Label, type, and Explanation. A short version of the text file is attached below (the number of variables is more than 300). The want dataset (shown in Excel, but&amp;nbsp;.sas7bdat is desired) is also included. Thank you so much for your time.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2024 19:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922619#M363297</guid>
      <dc:creator>CHL0320</dc:creator>
      <dc:date>2024-04-02T19:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922625#M363299</link>
      <description>&lt;P&gt;This works but I had to use the ancient CAPS option, because I don't know how to get input @'String' input to ignore case.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options caps=1;
data want;
   infile cards;

   input @'VAR NAME: '     var_name:$32.
         @'LABEL: '        label &amp;amp;$128.
         @'TYPE: '         type :$8.
         @'EXPLANATION: '  Explanation &amp;amp;$128.
         ;
   cards4;
Clinical Trial - Data Base Codebook
-----------------------------------------------------
Form name:QUESTIONNAIRE- PART B
Form code: BL
SAS datafile name: bu

NOTE:  First version - v40.0 - codebook

Versions:
v41.0 - Second version
        Created:
   Uploaded:


Codebook last updated:

---------------------------------------------------------------------------------
var name: HRBv1


Label: id

---------------------------------------------------------------------------------

                type:  numeric
                 range:  none
   length:
   format:
         missing code:

Explanation: a number given by the system.
Source: Automatically entered from computerized system
name of variable in Dump file:   id

-------------------------------------------------------------------------------
var name: HRBv2

Label: qs_checklist_name

-------------------------------------------------------------------------------

                  type:  string
      length: 15
      format: varchar

           missing code:


explanation: information about the visit: bl, number.
Source: Automatically entered from computerized system
name of variable in Dump file:   qs_checklist_name

-------------------------------------------------------------------------------
var name: HRBv3
Label: pdf_file_size


-------------------------------------------------------------------------------

                 type:  numeric
                 range:
      length:  20
      format: varchar
           missing code:

explanation: size of the pdf file in bytes.
Source: Automatically entered from computerized system
name of variable in Dump file:   pdf_file_size


-------------------------------------------------------------------------------
var name: HRBv4
Label: pdf_file_status

-------------------------------------------------------------------------------

                  type:  numeric (byte)

                 range:  [0,2]
      length:  1
      format: char
           missing code:   0


explanation: status of the pdf file: 0=blank, 1=draft, 2=final.
Source: Automatically entered from computerized system
name of variable in Dump file:   pdf_file_status
-------------------------------------------------------------------------------
var name: HRBv5
Label: user_pin
-------------------------------------------------------------------------------
type:  numeric (int)

                 range:
      length: 4
      format: integer
           missing code:

explanation: the user's pin number.
Source: Automatically entered from computerized system
name of variable in Dump file:   user_pin
;;;;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 690px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95097i9D05AA9DE4E39F3B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2024 20:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922625#M363299</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-04-02T20:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922631#M363300</link>
      <description>&lt;P&gt;Might explain to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/108652"&gt;@CHL0320&lt;/a&gt; that an INPUT statement using : Input @'some string value' moves the pointer in the input buffer to the next position where the value of 'some string value' exists and reads from there to get the value of the variable.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2024 20:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922631#M363300</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-04-02T20:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922644#M363307</link>
      <description>&lt;P&gt;Do you need "option caps=1"?&amp;nbsp; It looks like the&amp;nbsp;@ strings are consistent throughout the document.&amp;nbsp; So if you know the consistent case for each search string,&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;couldn't it be as below?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   input @'var name: '     var_name:$32.
         @'Label: '        label &amp;amp;$128.
         @'type: '         type :$8.
         @'Explanation: '  Explanation &amp;amp;$128.
         ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Without resorting to the CAPS option?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2024 23:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922644#M363307</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-02T23:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922648#M363309</link>
      <description>&lt;P&gt;Is your file representative of the actual file(s)?&amp;nbsp; It does appear pretty organized to me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If all of the variables have all of the elements (and the case is consistent) then you could just use&amp;nbsp;@'text' input style to jump to the location of the next metadata field.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you might try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data codebook;
  infile 'c:\downloads\have.txt' lrecl=132 pad ;
  if _n_=1 then do;
    input @'Form name:' formname $80. 
          @'Form code:' formcode $10.
          @'SAS datafile name:' formsasname $10.
    ;
    retain form: ;
  end;
  varnum+1;
  input @'var name:' varname $32.
        @'Label:' label $100.
        @'type:' type $20.
        @'range:' range $30.
        @'length:' length $20.
        @'format:' format $32.
        @'missing code:' missingcode $50.
  ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1712106058615.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95103i3334415BB85EB8EF/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1712106058615.png" alt="Tom_0-1712106058615.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If some of the elements (like LABEL for example) is not there then you might need to work a little harder.&amp;nbsp; Basically read each line and figure out if it one of interest and then extract the value you want from it.&amp;nbsp; Retain the values and then detect when a variable's definition ends (looks like that is signaled by a line of hyphens) and output the record and clear the retained variables.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 01:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922648#M363309</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-03T01:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922722#M363337</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something to handle case was needed. In the posted example the first two sets have "explanation" and "Explanation" .&lt;/P&gt;
&lt;P&gt;Which would make me extremely cautious about the remainder of the file and the key words.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Do you need "option caps=1"?&amp;nbsp; It looks like the&amp;nbsp;@ strings are consistent throughout the document.&amp;nbsp; So if you know the consistent case for each search string,&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;couldn't it be as below?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   input @'var name: '     var_name:$32.
         @'Label: '        label &amp;amp;$128.
         @'type: '         type :$8.
         @'Explanation: '  Explanation &amp;amp;$128.
         ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Without resorting to the CAPS option?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 13:52:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922722#M363337</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-04-03T13:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922724#M363339</link>
      <description>&lt;P&gt;Thank you for all your help. There is a problem with the consistency in variable cases. For example, I need to have a variable "explanation" to be included but some of them are "Explantation".&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 14:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922724#M363339</guid>
      <dc:creator>CHL0320</dc:creator>
      <dc:date>2024-04-03T14:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922728#M363342</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/108652"&gt;@CHL0320&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for all your help. There is a problem with the consistency in variable cases. For example, I need to have a variable "explanation" to be included but some of them are "Explantation".&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt; 's use of the system option CAPS handle. It translates all characters to upper case so the comparison is consistent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or talk to whoever prepares that file and fix why the case changes.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 14:25:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922728#M363342</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-04-03T14:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read unorganized text file to SAS dataset with selected variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922738#M363343</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/108652"&gt;@CHL0320&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for all your help. There is a problem with the consistency in variable cases. For example, I need to have a variable "explanation" to be included but some of them are "Explantation".&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you need to read those fields (why?) then you must go with the more complicate approach.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this that reads the tag text and cleans it up before deciding what metadata variable it means.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data codebook;
  infile 'c:\downloads\have.txt' dsd dlm=':' truncover end=eof;
  length formname $20 formcode $10
         varnum 8 varname $32
/* Define the other variables */
         explanation $200
  ;
  retain varnum 1 formname -- explanation;
  input tag :$200. @ ;
  tag=lowcase(compress(tag,'20A0090a0d'x));
  drop tag ;
  if index(_infile_,':') then select (tag);
    when ('varname')  do;
      if varname ne ' ' then do;
         output;
         varnum+1;
         call missing(of varname--explanation);
      end;
      input varname $200.;
    end;
    when ('formname') input formname $200.;
    when ('formcode') input formcode $200.;
    when ('explanation') input explanation $200.;
/* fill in the other possible variables */
    otherwise ;
  end;
  if eof and varname ne ' ' then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1712155703124.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95125i9F1CA235C0C308A2/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1712155703124.png" alt="Tom_0-1712155703124.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If it is possible that a metadata field value could span multiple lines you will need more than just the simple INPUT statement in the WHEN clause for that metadata field.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 14:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-unorganized-text-file-to-SAS-dataset-with/m-p/922738#M363343</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-03T14:51:49Z</dc:date>
    </item>
  </channel>
</rss>

