From: Alex on 10 Mar 2010 04:16 On Mar 10, 5:33 am, Henry <chchanghe...(a)gmail.com> wrote: > Hello there, > > I was wondering if there is a function that can help us to identify if > there is a letter (no matter which one from A-Z) in a variable. Of > course the variable will be defined as character since it contains > some letters in some observations. So, just wonder if there is a > function to identify it as 0 or 1? In other words, can I get a new > indicator = 1 if the variable "answer" contains any letter from A to > Z. and = 0 if not containing any letter? Thanks a lot! > > DATA OLD; > INPUT ID $ 1-3 > ANSWER $ 5-9; > DATALINES; > 001 ACBED > 002 11 > 003 12 > 004 zx > ; Hi Henry, Try out the anyalpha() function. It returns the first position at which a character is found in the string you pass to it. In your case you can use it like this in order to get a flag indicating whether ANSWER contains a letter or not: DATA OLD; INPUT ID $ 1-3 ANSWER $ 5-9; ALPHA = ( anyalpha( ANSWER ) > 0 ); DATALINES; 001 ACBED 002 11 003 12 004 zx 005 1a ; run; proc print; run; Note that I added record 005, which contains a letter at the 2nd position. Cheers, Alex
From: Henry on 10 Mar 2010 13:05 Hi Alex, Thanks for your help! On Mar 10, 2:16 am, Alex <alexander.k...(a)iea-dpc.de> wrote: > On Mar 10, 5:33 am, Henry <chchanghe...(a)gmail.com> wrote: > > > > > Hello there, > > > I was wondering if there is a function that can help us to identify if > > there is a letter (no matter which one from A-Z) in a variable. Of > > course the variable will be defined as character since it contains > > some letters in some observations. So, just wonder if there is a > > function to identify it as 0 or 1? In other words, can I get a new > > indicator = 1 if the variable "answer" contains any letter from A to > > Z. and = 0 if not containing any letter? Thanks a lot! > > > DATA OLD; > > INPUT ID $ 1-3 > > ANSWER $ 5-9; > > DATALINES; > > 001 ACBED > > 002 11 > > 003 12 > > 004 zx > > ; > > Hi Henry, > > Try out the anyalpha() function. It returns the first position at > which a character is found in the string you pass to it. In your case > you can use it like this in order to get a flag indicating whether > ANSWER contains a letter or not: > > DATA OLD; > INPUT ID $ 1-3 > ANSWER $ 5-9; > > ALPHA = ( anyalpha( ANSWER ) > 0 ); > > DATALINES; > 001 ACBED > 002 11 > 003 12 > 004 zx > 005 1a > ; > run; > proc print; > run; > > Note that I added record 005, which contains a letter at the 2nd > position. > > Cheers, > Alex
|
Pages: 1 Prev: Data Manipulation Next: Test for the equality of coefficients |