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;
It is surprising that TOKEN
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.
Astounding wrote:
It is surprising that TOKEN
Actually it's not all surprising that (of array-name
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;
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.
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.