Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Urgent question about string

Status
Not open for further replies.

iren

Technical User
Mar 8, 2005
106
US
I have ID ADAM19480204M
(name+year+month+day+gender)
I need to rebuid it in order it looks like the following:
ADAM194802M (name+year+month+gender)

(Name can vary up to 5 letters)
In other words I need to take out DAY part.
Could you give me a hand, please , with this code?
Thank you in advance,
 
Hmmm.
That's a fiddly one, however it shouldn't be too difficult.
You need a selection of functions here to achieve your goal.
substr(<variable>,<start position>,<length>)
length(<variable>)
compress(<variable>,<characters to remove>)

The way I'd approach this is to first find the length of the field, then cut the gender out.
Code:
  length = length(ID);
  gender = substr(ID,length,1);
  ID2 = substr(ID,1,length-1);
This should give you a new ID field without the gender on the end. Now, if you get rid of any numbers in the field, you'll just be left with the name.
Code:
  name = compress(ID2,'1234567890');
  name_length = length(name);
Now we want to get the date bits out.
Code:
  year  = substr(ID2,name_length + 1,4);
  month = substr(ID2,name_length + 5,2);
  day   = substr(ID2,name_length + 7,2);
Now all you need to do is concatenate the bits you want back together again.
You might need to fiddle with the numbers a little to get things lined up correctly, I frequently get them wrong first time, so check that carefully.



Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top