SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
Mike_Davis
Fluorite | Level 6

Helo everyone,

I have the SAS code:

data one;

a='ABCDE';

LEN=length(a);

array aa

  • $ x1-x5;
  • do i=1 to length(a);

    j=substr(a,i,1);

    aa=j;

    end;

    drop i j len;

    run;

    but I want to change the code to make use of variable LEN as part of definition of array , like:(I don't want to use x1-x5 here)

    data one;

    a='ABCDE';

    LEN=length(a);

    array aa

  • $ x1-x||length(a);/***Here is the problem*****/
  • do i=1 to length(a);

    j=substr(a,i,1);

    aa=j;

    end;

    drop i j len;

    run;

    Please advise me ,

    Thanks!

    Mike

    9 REPLIES 9
    art297
    Opal | Level 21

    Mike,

    One way of doing what you want to do is to pass the needed information off to a call execute.  e.g.:

    data one;

      a='ABCDE';

      CALL EXECUTE (

      'data one;

        set one;

        array aa

  • $ x1-x' || LEFT(PUT(length(a),2.)) || ';'
  •     ||'do i=1 to length(a);

          j=substr(a,i,1);

          aa=j;

        end;

        drop i j;

      run;'

      );

    run;

    Mike_Davis
    Fluorite | Level 6

    Thank you Art,

    Very impressive  method!

    In my understanding,Does the essence of the call execute is treating these data step parts as strings and doing character operation such as using "||" to connect strings. then execute it?

    Thanks

    Mike

    art297
    Opal | Level 21

    Yes!  It executes as soon as the current datastep ends.

    MikeZdeb
    Rhodochrosite | Level 12

    hi ... another idea (works like CALL EXECUTE without using CALL EXECUTE) ...

    %let a=ABCDE;


    filename x temp;


    data _null_;

    a = "&a";

    l = length(a);

    file x;

    put 'data one; a="&a"; array aa(*) $1 x1-x' l '; do j=1 to dim(aa); aa(j)=char(a,j); end; keep x:; run;';

    run;


    %include x;

    x1    x2    x3    x4    x5

    A     B     C     D     E


    %let a=MIKE.DAVIS;

    x1    x2    x3    x4    x5    x6    x7    x8    x9    x10

    M     I     K     E     .     D     A     V     I      S

    Howles
    Quartz | Level 8

    LEN and A cannot vary, so make them literals.

    %let a = ABCDE ;

    %let LEN = %sysfunc( length(ABCDE) ) ;

    data one;

    array aa

  • $ x1-x&LEN. ;
  • do i=1 to &LEN. ;

    j=substr("&a",i,1);

    aa=j;

    end;

    drop i j ;

    run;

    Ksharp
    Super User

    What result do you want ? I think Hash Table maybe is a good choice.

    Ksharp

    Mike_Davis
    Fluorite | Level 6

    Thank you Ksharp!

    I will be very appreciate if you run the SAS code as follow:


    data one;

    a='ABCDE';

    LEN=length(a);

    array aa

  • $ x1-x5;
  • do i=1 to length(a);

    j=substr(a,i,1);

    aa=j;

    end;

    drop i j len;

    run;

    my expect result is I don't want to use " array aa

  • $ x1-x5;"
    I want to use a variable instead of an exact number to express the up-bond of the array.
    such as I want use:"array aa
  • $ x1-x||length("ABCDE");"

  • I know hash is a very nice tool for join tables but I don't have any sense of making a connection between this problem with hash,

    This sounds very interesting to me.

    Would you like to share your idea with me?


    I am very appreciate about it!


    Thanks a lot!


    Mike

    Patrick
    Opal | Level 21

    A SAS array is defined during compilation time of a data step and the number of array elements is fix. The length() statement is executed during execution time of a data step. That's why your approach can't work.

    It might be worth that you explain us what problem you try to solve.

    I can only guess what KSharp has in mind but I assume it's about having your data in a long structure (so just adding elements to a hash during execution time) instead of having your data in a wide structure using an array.

    Are you eventually after something like below?

    data one(drop=_:);
      length element $1;
      rownum+1;

      input _a $;
      do _i=1 to length(_a);
        element=substr(_a,_i,1);
        output;
      end;

      datalines;
    ABCDE
    FGHIJKL
    MN
    ;
    run;

    proc transpose data=one out=one(drop=_:) prefix=X;
      by rownum;
      var element;
    run;

    Ksharp
    Super User

    So if you want varying number of variables , I recommend to use Patrick's code PROC TRANSPOSE.

    Ksharp

    sas-innovate-white.png

    Our biggest data and AI event of the year.

    Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

    Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    Find more tutorials on the SAS Users YouTube channel.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

    Discussion stats
    • 9 replies
    • 2310 views
    • 6 likes
    • 6 in conversation