BookmarkSubscribeRSS Feed
Maisha_Huq
Quartz | Level 8

Hello!

I have a character variable for address which I'd like to use to create new variables for just the full address's

- street address

-city

-state

-zip

I am doing this using the substring function (i.e. state=substr(addr,length(addr)-1,2); )

however, the data is always entered so there is only space between the street address and city instead of, say, a comma.

Would anyone have suggestions for how I may go about creating the street address and city variables then?

The help is appreciated.

4 REPLIES 4
art297
Opal | Level 21

If your data are really that consistent then you might be able to use something like:

data have;

  informat street $100.;

  length city $30.;

  input street &;

  city=scan(substr(_infile_,length(street)+1,),1,',');

  state=scan(_infile_,-2,' ');

  zip=scan(_infile_,-1,' ');

  cards;

4552 east 32nd street, apt 4  new york, ny  12209-1234

23 nearby blvd.  pittsburgh, pa  15217

;

art297
Opal | Level 21

: Here is another thought. Depending upon the accuracy of your city names, you might be able to get around the various problems as follows:

data have;

  length address $150;

  input;

  address=_infile_;

  cards;

4552 east 32nd street, apt 4 albany, ny 12209-1234

23 nearby blvd. pittsburgh, pa 15217

12345 west   north street, apt 12 (3rd floor) Arlington Heights, IL 60004-6667

;

filename dummy temp;

data _null_;

   file dummy;

   set have;

   put address;

run;

data want (drop=citystate);

  informat street $100.;

  informat citystate $50.;

  length city $30;

  length state $3;

  informat zip $10.;

  infile dummy;

  input;

  _infile_=compbl(translate(_infile_,' ','09'x));

  zip=scan(_infile_,-1,' ');

  citystate=zipcity(substr(zip,1,5));

  city=scan(citystate,1,',');

  state=scan(citystate,2,',');

  street=substr(_infile_,1,find(upcase(_infile_),strip(upcase(city)))-1);

run;

Maisha_Huq
Quartz | Level 8

Hi Arthur,

I will try this!  Something I left out, though:  is there a way to extract a string (which we know is all uppercase) between two specific delimiters in SAS (i.e. my two delimiters are a double space and a comma)?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

For fixed format strings you may take some time to read up on perl regular expressions.  It could save you some time writing all the necessary if thens.  http://www.cs.tut.fi/~jkorpela/perl/regexp.html

I fyou are still going to use string functions, then look at index().  So if I had:

001  ABC, 12

And I do substr(string,index(string,"  ")+2,index(string,",") - (index(string,"  ")+2));

This means start taking characters from where I find two spaces (+2 so I don't get the spaces) up to where I find the comma (less two for the original spaces).

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 9426 views
  • 6 likes
  • 3 in conversation