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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

9 REPLIES 9
Reeza
Super User

Will the string always be the same length?

Haikuo
Onyx | Level 15

Congratulations, Reeza! for entering the 10K club!

Haikuo

stat_sas
Ammonite | Level 13

Hi Reeza,

We may have a variable length for few observarions.

Regards

data_null__
Jade | Level 19

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   
    RW9
    Diamond | Level 26 RW9
    Diamond | Level 26

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

    stat_sas
    Ammonite | Level 13

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

    Regards,

    data_null__
    Jade | Level 19

    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;
    Haikuo
    Onyx | Level 15

    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

    RW9
    Diamond | Level 26 RW9
    Diamond | Level 26

    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;

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    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
    • 9 replies
    • 781 views
    • 8 likes
    • 5 in conversation