Text extraction from a string

Accepted Solution Solved
Reply
Trusted Advisor
Posts: 1,228
Accepted Solution

Text extraction from a string

Hi Everyone,

I've a dataset "have " and want to get ouptut given below. Any suggestions regarding this please?

Regards,

Naeem

data have;

infile datalines;

input str $;

datalines;

abcdefgh

bcdeghfa

fghedcbb

ehgbcdae

;

output.

str             col1   col2  col3  col4  col5  col6  col7  col8

abcdefgh   a       b        c      d      e       f         g      h

bcdeghfa   b       c        d      e      g       h        f       a

fghedcbb   f        g        h      e      d       c        b      b

ehgbcdae  e       h        g      b      c       d        a      e


Accepted Solutions
Solution
‎04-30-2014 11:03 AM
Respected Advisor
Posts: 3,156

Re: Text extraction from a string

Here is one way. An arbitrary array dimension has been set to 20, but you can also choose the length of the targeting variable as the dimension, that information can be obtained using dictionary tables.

data have;

infile datalines;

input str $;

datalines;

abcdefgh

bcdeghfa

fghedcbb

ehgbcdae

;

data want;

set have;

array col(20) $ 1;

do _n_=1 to lengthn(str);

col(_n_)=char(str,_n_);

end;

  run;

Haikuo

View solution in original post


All Replies
Super User
Posts: 19,770

Re: Text extraction from a string

Will the string always be the same length?

Respected Advisor
Posts: 3,156

Re: Text extraction from a string

Congratulations, Reeza! for entering the 10K club!

Haikuo

Trusted Advisor
Posts: 1,228

Re: Text extraction from a string

Hi Reeza,

We may have a variable length for few observarions.

Regards

Respected Advisor
Posts: 3,799

Re: Text extraction from a string

You can do mass initialization using direct memory access.

data have;
   infile datalines;
  
input str $;
   array col[8] $1;
  
if _n_ eq 1 then addr=addrlong(col[1]);
   retain addr; drop addr;
   *CALL POKELONG(source,pointer<,length><,floating-point> );
  
call pokelong(str,addr,min(dim(col),length(str)));
   putlog 'NOTE: ' str= col
  • ;
  •    datalines;
    abcdefgh
    bcdeghfa
    fghedcbb
    ehgbcdae
    bcdhfa
    fghb
    ;;;;
       run;

    NOTE: str=abcdefgh a b c d e f g h
    NOTE: str=bcdeghfa b c d e g h f a
    NOTE: str=fghedcbb f g h e d c b b
    NOTE: str=ehgbcdae e h g b c d a e
    NOTE: str=bcdhfa b c d h f a   
    NOTE: str=fghb f g h b   
    Super User
    Super User
    Posts: 7,942

    Re: Text extraction from a string

    Posted in reply to data_null__

    Sweet, peek and poke good ol' ZX81 days all over again.  Will have to investigate the pokelong function.

    Trusted Advisor
    Posts: 1,228

    Re: Text extraction from a string

    Thanks experts - I really appreciate your contribution. This is more than enough for my learning.

    Regards,

    Respected Advisor
    Posts: 3,799

    Re: Text extraction from a string

    One more technique sometimes called INFILE magic.

    data have;
       input str $;
       datalines;
    abcdefgh
    bcdeghfa
    fghedcbb
    ehgbcdae
    bcdhfa
    fghb
    ;;;;
       run;
    data need2;
       infile cards;
      
    input @;
       do while(not eof);
          set have end=eof;
          _infile_ = str;
         
    input @1 (col1-col8)($1.) @;
          output;
         
    end;
      
    stop;
      
    cards;
    necessary evil
    ;;;;
       run;
    proc print;
      
    run;
    Solution
    ‎04-30-2014 11:03 AM
    Respected Advisor
    Posts: 3,156

    Re: Text extraction from a string

    Here is one way. An arbitrary array dimension has been set to 20, but you can also choose the length of the targeting variable as the dimension, that information can be obtained using dictionary tables.

    data have;

    infile datalines;

    input str $;

    datalines;

    abcdefgh

    bcdeghfa

    fghedcbb

    ehgbcdae

    ;

    data want;

    set have;

    array col(20) $ 1;

    do _n_=1 to lengthn(str);

    col(_n_)=char(str,_n_);

    end;

      run;

    Haikuo

    Super User
    Super User
    Posts: 7,942

    Re: Text extraction from a string

    Well, quick and dirty method using macro:

    data have;
    infile datalines;
    input str $;
    datalines;
    abcdefgh
    bcdeghfa
    fghedcbb
    ehgbcdae
    ;
    run;

    proc sql;
      select  LENGTH
      into    :MAX_LEN
      from    SASHELP.VCOLUMN
      where   LIBNAME="WORK"
          and MEMNAME="HAVE";
    quit;

    %macro Parse ();
      data want;
        set have;
        array col{&MAX_LEN.} $1.;
        %do i=1 %to &MAX_LEN.;
          col&i.=substr(str,&i.,1);
        %end;
      run;
    %mend Parse;
    %Parse;

    🔒 This topic is solved and locked.

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

    Discussion stats
    • 9 replies
    • 320 views
    • 8 likes
    • 5 in conversation