%MACRO parseit ( /* note 2 positional parms */ s, /* name of the string variable to be parsed */ n, /* number of the word to be located. if value is 99 THEN MACRO COUNTS THE WORDS AND RETURNS *LAST* WORD AND LOCATION */ first=1, /* specify first=0 if NOT the first invocation in data step */ debug=0); /* as usual, debug=1 to get extra diagnostics. not for users*/ %* --- See parse.txt in the same macro library for exaplanation of macro --; %*--Here is the code---------------------------------; %if &first %then %do; length __s $200 _word $200 _parserr $64 _locword _lenword 8 _nwords 8; drop __s _word _parserr _locword _lenword _nwords; %end; __s=&s; if __s=' ' then do; _word=' '; _locword=0; _lenword=0; _nwords=0; _parserr='1 blank string'; end; else do; do _nwords=1 to &n while(__s ne ' '); if _nwords eq 1 then _locword=1; else _locword=_locword + _lenword +1; *--length of prev word +1; do while(substr(__s,1,1)=' '); *--left-justify-; __s=substr(__s,2); _locword+1; %if &debug %then %str(put '*** blank removed *** ';); end; _word=scan(__s,1); _lenword=length(_word); if _nwords=&n or substr(__s,_lenword+1)=' ' then leave; __s=substr(__s,_lenword+2); %if &debug %then %str( put _all_;); end; *--_nwords loop--; if _nwords lt &n and &n ne 99 then do; _parserr='2 not enough words'; _word=' '; _lenword=0; _locword=0; end; %if &debug %then %do; put / _all_; %end; end; *--else do -----*; %mend parseit;