BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
YadiAcho
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
Tom
Super User Tom
Super User

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;

YadiAcho
Calcite | Level 5

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.....

Tom
Super User Tom
Super User

?? 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

Ksharp
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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