f



Read a variable from Right to Left

Hi All,

I want help to substring a variable from right to left, for eg: I have
a SAS data set which has a variable Address like.

Address

114, Mills Rd, Apt 202
345 Morris Drive, Apt 345
Chestnut Avenue Apt # 987

Is there a way I can create a variable say Type like

Type

Apt202
Apt345
pt#987

Thanks,
Richie

0
gsingh24 (36)
3/21/2006 10:53:59 PM
comp.soft-sys.sas 142828 articles. 3 followers. Post Follow

7 Replies
573 Views

Similar Articles

[PageSpeed] 39

Richie:

  Try the following.  I assume there is a comma before "Apt # 987" in
your third address.  Output follows the code.

data temp;
  input;
  Type=scan(_infile_,-1,",");
  datalines;
114, Mills Rd, Apt 202
345 Morris Drive, Apt 345
Chestnut Avenue, Apt # 987
;
run;

proc print;
run;

***** Output *****
                                          The SAS System         15:09
Tuesday, March 21, 2006   2

                                         Obs      Type

                                          1     Apt 202
                                          2     Apt 345
                                          3     Apt # 987


J S Huang
1-515-557-3987
fax 1-515-557-2422

>>> Richie <gsingh24@GMAIL.COM> 3/21/2006 4:53:59 PM >>>
Hi All,

I want help to substring a variable from right to left, for eg: I have
a SAS data set which has a variable Address like.

Address

114, Mills Rd, Apt 202
345 Morris Drive, Apt 345
Chestnut Avenue Apt # 987

Is there a way I can create a variable say Type like

Type

Apt202
Apt345
pt#987

Thanks,
Richie
0
3/21/2006 11:02:33 PM
Richie,

Are you trying to squeeze everything in the end part to fit into six
characters? I am guessing this based on the sample output. If these are all
really addresses then you may not always have apartment numbers. Assuming
your question is more general, here is a solution.

data _null_ ;
  length string1 $100 type $6 ;
  infile cards truncover ;
  input @1 string1 $char100. ;
  type = substr(compress(string1),length(compress(string1))-5) ;
  put string1 = type = ;
  cards ;
114, Mills Rd, Apt 202
345 Morris Drive, Apt 345
Chestnut Avenue Apt # 987
run ;

Venky Chakravarthy

On Tue, 21 Mar 2006 14:53:59 -0800, Richie <gsingh24@GMAIL.COM> wrote:

>Hi All,
>
>I want help to substring a variable from right to left, for eg: I have
>a SAS data set which has a variable Address like.
>
>Address
>
>114, Mills Rd, Apt 202
>345 Morris Drive, Apt 345
>Chestnut Avenue Apt # 987
>
>Is there a way I can create a variable say Type like
>
>Type
>
>Apt202
>Apt345
>pt#987
>
>Thanks,
>Richie
0
swovcc (579)
3/21/2006 11:47:46 PM
Richie,

How about something like this

data temp;
input @ 'Apt' apt $10.;
cards;
114, Mills Rd, Apt 202
345 Morris Drive, Apt 345
Chestnut Avenue Apt # 987
;
run;

Cheers

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
                                          Obs     apt

                                             1     202
                                             2     345
                                             3     # 987

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

0
makwana (75)
3/22/2006 12:12:44 AM
On Tue, 21 Mar 2006 14:53:59 -0800, Richie <gsingh24@GMAIL.COM> wrote:

>Hi All,
>
>I want help to substring a variable from right to left, for eg: I have
>a SAS data set which has a variable Address like.
>
>Address
>
>114, Mills Rd, Apt 202
>345 Morris Drive, Apt 345
>Chestnut Avenue Apt # 987
>
>Is there a way I can create a variable say Type like
>
>Type
>
>Apt202
>Apt345
>pt#987
>
>Thanks,
>Richie

The simple answer is to provide a negative value for the
second parameter of the scan() function.
data demo ;
  string = '345 Morris Drive, Apt 345' ;
  last_word   = scan( string, -1 ) ;
  second_last = scan( string, -2 ) ;
  put (_all_)(=) ;
run;

The log shows
string=345 Morris Drive, Apt 345 last_word=345 second_last=Apt


See the online doc for optional third parameter which allows
you to specify what punctuation to (dis)allow, like that '#'

Peter
0
3/22/2006 8:47:52 AM
simply use

Type = reverse(substr(reverse(compress(Address)),1,6)) ;

HTH

0
chrisj75 (96)
3/22/2006 9:39:50 AM
Assuming you have V9, you could look at using PERL Regular Expressions
- the PRXPARSE function.

See Ron Cody's NESUG article:
http://www.nesug.org/html/Proceedings/nesug03/bt/bt002.pdf

Because there are probably many variations of "apt" in your address
field, you migh start with something like this:

data whatever;
   set addresses;
   if _n_ = 1 then
   do;
      retain patternid;
      /* The i option specifies a case insensitive search. */
      pattern = "/apt|apt #|apt.|apt. #|, apt|#/i"; /* etc. */
      patternid = prxparse(pattern);
   end;
   call prxsubstr(patternid, address, position, length);
   if position ^= 0 then
   do;
      match = substr(address, position, length);
      /* or match = substr(address, position); */
      put match:$QUOTE. "found in " address:$QUOTE.;
   end;
run;

Of course, any approach would result in endless tinkering...

0
breading (6)
3/22/2006 5:37:08 PM
If the variable seperated by comma, then you can try the below:
data add;
	input address $30.;
	apartment=scan(address,-1,',');
	cards;
	114,Mills Rd,Apt 202
	345,Morris Drive,Apt 345
	678,Chestnut Avenue,Apt# 987
	;
 run;

 proc print;
 run;

Richie wrote:
> Hi All,
>
> I want help to substring a variable from right to left, for eg: I have
> a SAS data set which has a variable Address like.
>
> Address
>
> 114, Mills Rd, Apt 202
> 345 Morris Drive, Apt 345
> Chestnut Avenue Apt # 987
>
> Is there a way I can create a variable say Type like
> 
> Type
> 
> Apt202
> Apt345
> pt#987
> 
> Thanks,
> Richie

0
jato88 (14)
3/23/2006 5:38:55 AM
Reply: