BookmarkSubscribeRSS Feed
bob_pearson
Calcite | Level 5

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;

    3 REPLIES 3
    Astounding
    PROC Star

    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.

    data_null__
    Jade | Level 19

    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.
  • bob_pearson
    Calcite | Level 5

    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;

    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
    • 3 replies
    • 1858 views
    • 0 likes
    • 3 in conversation