DATA Step, Macro, Functions and more

Array in Function, Reference a subset

Reply
Occasional Contributor
Posts: 9

Array in Function, Reference a subset

Hi all,

Is there a way to reference a subset of an array in a function call? The code below is a stripped down version of what I'm working with. What I'm looking for is something like the commented out LastN assignments at the bottom, which I know don't work. The number of tokens may vary, so the hard-coded reference in the uncommented LastN won't do it. I can loop through the last N words and sequentially add one token at a time, but I thought there might be a more direct method using the OF operator such as in the "all" assignment. Any ideas?

Bob

data a;

    input string $30.;

    cards;

adam doug bob craig

pete repeat george regeorge

;

run;

data b;

    set a;

    if _n_=1 then do;

        array token {9} $15 _temporary_;

        ** other stuff;

    end;

    nTokens = countw(string);

    do _i=1 to nTokens;

        token[_i] = scan(string, _i);

    end;

    first = token{1};

    all = catx('-', of token

  • );
  •     lastN = catx('-', token{2}, token{3}, token{4})

    *    lastN2 = catx('-', of token{2:4});

    *    lastN3 = catx('-', of token{2}-token{4});

    ;

    run;

    Super User
    Posts: 5,085

    Re: Array in Function, Reference a subset

    It is surprising that TOKEN

  • works, but the others don't.  At any rate, you can construct LASTN based on ALL:
  • lastN4 = catx('-', scan(all, '-', -3), scan(all, '-', -2), scan(all, '-', -1));

    That should work, as long as none of the tokens contain a dash.  That shouldn't be a problem, seeing how the individual tokens are created by SCAN with the default set of delimiters.

    Finally, BEWARE.  Your code will give you the wrong result when the number of tokens decreases from one observation to the next.  In a temporary array, the values are automatically retained.  So with 4 tokens on the current observation, you will still have the retained value for token[5] if a value was assigned on any previous observation.

    Good luck.

    Respected Advisor
    Posts: 3,777

    Re: Array in Function, Reference a subset

    Astounding wrote:

    It is surprising that TOKEN

  • works, but the others don't.  At any rate, you can construct LASTN based on ALL:
  • Actually it's not all surprising that (of array-name

  • ) works because that is a kind of SAS variable list. I believe the feature for temporary arrays was added in 9.2.  It would be nice if the other forms worked in OF lists.   Probably something to do with compile vs execute.
  • Occasional Contributor
    Posts: 9

    Re: Array in Function, Reference a subset

    I've ended up doing the DO loop method just to be safe. Would love to use the OF syntax though if it ever comes. Thanks for the input.

    do _i=2 to nTokens;

        lastN= catx(' ', lastN, token{_i});

    end;

    Ask a Question
    Discussion stats
    • 3 replies
    • 466 views
    • 0 likes
    • 3 in conversation