DATA Step, Macro, Functions and more

reading txt file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

reading txt file

Hi All,

I have problem convert the text....

Herewith the text look like:

1  alpha5  beta nano 123 benga

   AR2313 A    MA   D   21AUG2011

2  dexa   tito loli 342 dunga

   NN6112 A    MA   F   12AUG2011

3  alpha4  beta nano 123 benga

   BB2311 A    MA   D   21AUG2011

4  dexa   tito loli 342 dunga

   DW2312 A    MAT  F   12AUG2011

   PARMA

5  alpha3  beta nano 123 benga

   MW1223 A    MLA  D   21AUG2011

6  dexa2   tito loli 342 dunga

   FR2132 A    GA   F   12AUG2011

7  alpha1  beta nano 123 benga

   LG1321 A    MA   D   21AUG2011

8  dexa   tito loli 342 dunga

   PP2321 A    MA   F   12AUG2011

This is the code i try:

data Read;

  infile "E:\sasdata\raw\test1.txt" end=gakada;

  format d1 mmddyy10.;

  input @1 cekline $3. @;

  if cekline ne " " then do;

    if _n_ gt 1 then output;

        input @1 SEQ nm1: $6. nm2: $4. nm3: $5. code: 3. nm4: $5. @;

        retain SEQ nm1 nm2 nm3 code nm4;

    if cekline = "" then output;

        input / @4 lx1: $7.

              @11 type: $6. @16 apv: $3. @21 result: $3. @25 d1: date9.;

        retain  lx1 type apv result d1;

  end;

  if gakada then output;

run;

this is the result:

Obs          d1  rectype  SEQ   nm1    nm2   nm3   code   nm4    lx1    type  apv  result

  1   08/21/2011     2      1   alpha5  beta  nano   123  benga  AR2313   A    MA     D

  2   08/12/2011     3      2   dexa    tito  loli   342  dunga  NN6112   A    MA     F

  3   08/21/2011     4      3   alpha4  beta  nano   123  benga  BB2311   A    MA     D

  4   08/12/2011     5      4   dexa    tito  loli   342  dunga  DW2312   A    MAT    F

  5   08/21/2011     6      5   alpha3  beta  nano   123  benga  MW1223   A    MLA    D

  6   08/12/2011     7      6   dexa2   tito  loli   342  dunga  FR2132   A    GA     F

  7   08/21/2011     8      7   alpha1  beta  nano   123  benga  LG1321   A    MA     D

  8   08/12/2011     8      8   dexa    tito  loli   342  dunga  PP2321   A    MA     F

My problem is, there is 1 line lost from the code for observation #4 (PARMA) need one more variabel.....

Anyone can help me??

thx and regards,

Acho

Attachment
Attachment

Accepted Solutions
Solution
‎10-08-2011 04:29 PM
Super User
Super User
Posts: 6,502

Re: reading txt file

What does the extra line represent?

Is it another record for sequence number 4 ? 

data read;

  infile file1 end=eof truncover;

  length SEQ 8 nm1-nm3 $6 code 8 nm4 $6 ;

  length lx1 $7 type $6  apv $3 result $3  d1 8;

  format d1 mmddyy10.;

  informat d1 date9. ;

  retain SEQ nm1 nm2 nm3 code nm4;

  drop check ;

  input @1 check $3. @;

  if check ne " " then do;

    input @1 SEQ nm1-nm3 code nm4 ;

  end;

  else do ;

    input @1 lx1 type apv result d1 ;

    output;

  end;

run;

Or is it an additional field ?  Perhaps a comment?

data read;

  infile file1 end=eof truncover;

  length SEQ 8 nm1-nm3 $6 code 8 nm4 $6 ;

  length lx1 $7 type $6  apv $3 result $3  d1 8;

  length comment $50;

  format d1 mmddyy10.;

  informat d1 date9. ;

  retain SEQ nm1 nm2 nm3 code nm4;

  drop check check2;

  input @1 check $3. @;

  if check ne " " then do;

    input @1 SEQ nm1-nm3 code nm4 ;

  end;

  else do ;

    input @1 lx1 type apv result d1 ;

    if not eof then do;

      input @1 check2 $3. @@;

      if check2 = ' ' then input comment;

    end;

    output;

  end;

run;

View solution in original post


All Replies
Solution
‎10-08-2011 04:29 PM
Super User
Super User
Posts: 6,502

Re: reading txt file

What does the extra line represent?

Is it another record for sequence number 4 ? 

data read;

  infile file1 end=eof truncover;

  length SEQ 8 nm1-nm3 $6 code 8 nm4 $6 ;

  length lx1 $7 type $6  apv $3 result $3  d1 8;

  format d1 mmddyy10.;

  informat d1 date9. ;

  retain SEQ nm1 nm2 nm3 code nm4;

  drop check ;

  input @1 check $3. @;

  if check ne " " then do;

    input @1 SEQ nm1-nm3 code nm4 ;

  end;

  else do ;

    input @1 lx1 type apv result d1 ;

    output;

  end;

run;

Or is it an additional field ?  Perhaps a comment?

data read;

  infile file1 end=eof truncover;

  length SEQ 8 nm1-nm3 $6 code 8 nm4 $6 ;

  length lx1 $7 type $6  apv $3 result $3  d1 8;

  length comment $50;

  format d1 mmddyy10.;

  informat d1 date9. ;

  retain SEQ nm1 nm2 nm3 code nm4;

  drop check check2;

  input @1 check $3. @;

  if check ne " " then do;

    input @1 SEQ nm1-nm3 code nm4 ;

  end;

  else do ;

    input @1 lx1 type apv result d1 ;

    if not eof then do;

      input @1 check2 $3. @@;

      if check2 = ' ' then input comment;

    end;

    output;

  end;

run;

Occasional Contributor
Posts: 8

Re: reading txt file

for observation number 4...  the code cant solve this line

4  dexa   tito loli 342 dunga

   DW2312 A    MAT  F   12AUG2011

   PARMA

I cant get PARMA.....

Super User
Super User
Posts: 6,502

Re: reading txt file

?? What do you want it to look like ??

With the first version it generates:

Obs    SEQ    nm1     nm2     nm3     code     nm4      lx1      type    apv    result            d1

  4      4     dexa    tito    loli     342    dunga    DW2312     A      MAT      F       08/12/2011

  5      4     dexa    tito    loli     342    dunga    PARMA                                       .

With the second version it generates:

Obs   SEQ   nm1    nm2    nm3    code    nm4     lx1     type   apv   result       d1       comment

  4     4    dexa   tito   loli    342   dunga   DW2312    A     MAT     F      08/12/2011    PARMA

Super User
Posts: 9,687

Re: reading txt file

I konw Tom have already given an answer. But I also want to try it.

data temp(drop=t);
infile datalines truncover  eof=eof;
input @1 (a1-a6) (: $12.) / (b1-b5) (: $12.)  ;
input t $char2. @@;
if missing(t)  then input x $ ;
eof: output;
datalines;
1  alpha5  beta nano 123 benga
   AR2313 A    MA   D   21AUG2011
2  dexa   tito loli 342 dunga
   NN6112 A    MA   F   12AUG2011
3  alpha4  beta nano 123 benga
   BB2311 A    MA   D   21AUG2011
4  dexa   tito loli 342 dunga
   DW2312 A    MAT  F   12AUG2011
   PARMA
5  alpha3  beta nano 123 benga
   MW1223 A    MLA  D   21AUG2011
6  dexa2   tito loli 342 dunga
   FR2132 A    GA   F   12AUG2011
7  alpha1  beta nano 123 benga
   LG1321 A    MA   D   21AUG2011
8  dexa   tito loli 342 dunga
   PP2321 A    MA   F   12AUG2011
;
run;

Ksharp

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 172 views
  • 1 like
  • 3 in conversation