Help using Base SAS procedures

How to make use of a variable in array definition

Reply
Regular Contributor
Posts: 222

How to make use of a variable in array definition

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 , likeSmiley SadI 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

    PROC Star
    Posts: 7,363

    Re: How to make use of a variable in array definition

    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;

    Regular Contributor
    Posts: 222

    Re: How to make use of a variable in array definition

    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

    PROC Star
    Posts: 7,363

    Re: How to make use of a variable in array definition

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

    Valued Guide
    Posts: 765

    Re: How to make use of a variable in array definition

    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

    Regular Contributor
    Posts: 184

    Re: How to make use of a variable in array definition

    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;

    Super User
    Posts: 9,682

    Re: How to make use of a variable in array definition

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

    Ksharp

    Regular Contributor
    Posts: 222

    Re: How to make use of a variable in array definition

    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

    Respected Advisor
    Posts: 3,896

    Re: How to make use of a variable in array definition

    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=_Smiley Happy;
      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=_Smiley Happy prefix=X;
      by rownum;
      var element;
    run;

    Super User
    Posts: 9,682

    Re: How to make use of a variable in array definition

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

    Ksharp

    Ask a Question
    Discussion stats
    • 9 replies
    • 306 views
    • 6 likes
    • 6 in conversation