LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (October 2007, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sun, 28 Oct 2007 22:37:35 -0400
Reply-To:     Ya Huang <ya.huang@AMYLIN.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Ya Huang <ya.huang@AMYLIN.COM>
Subject:      Re: Inverse of %SCAN for numeric position
Comments: To: OR Stats <stats112@GMAIL.COM>

Even in data step, there is no (at least for v8 as I know of) function that can get the order of the word in a string. You can always use do loop with scan() to get what you want, but that needs multiple lines of code. To get the order of a word in a string with a one-liner, we can use a trick similar to the one that counts the number of words in a string. The following code shows, step by step, how it can be done:

1 data _null_; 2 a='abc defg hi jlmn opqws t uv wxy'; 3 b=tranwrd(lowcase(a),'uv','z'); 4 put b=; 5 b=translate (b,'yyyyyyyyyyyyyyyyyyyyyyyyyz','abcdefghijklmnopqrstuvwxyz'); 6 put b=; 7 b=translate(compbl(b),' xz','y z'); 8 put b=; 9 b=compress(b); 10 put b=; 11 position=index(b,'z'); 12 put 'uv is the # ' position ' word in the string'; 13 run;

b=abc defg hi jlmn opqws t z wxy b=yyy yyyy yy yyyy yyyyy y z yyy b=x x x x x xzx x b=xxxxxxzxx uv is the # 7 word in the string

All these lines can be nested into a one-liner. Similarly, if you know how to use %sysfunc, you can make it a macro function.

On Sun, 28 Oct 2007 14:13:33 -0500, OR Stats <stats112@GMAIL.COM> wrote:

>Hi Everyone, Does SAS have a macro function that is the inverse of its >%SCAN function? The purpose is the following: > >The user of my macro would input his text. I would match the text with an >element in my array and return the position within the array. So that given >my array is declared as > >%let Array = Jack Jill Bob Anne; > >When the end-user inputs any one of the four names in my macro function, I >would like to have 1 returned if he typed in Jack; 2 if he typed Jill and so >forth. So while using SAS's %SCAN function, given the position, and the >function returns the name. I would like to have that function inversed so >that given the name, a SAS function returns the position of the element >within the array that matches the name. Does SAS have this function? > >Thank you!


Back to: Top of message | Previous page | Main SAS-L page