DATA Step, Macro, Functions and more

how to derive first name ,lastname,dob from a string

Reply
Frequent Contributor
Posts: 88

how to derive first name ,lastname,dob from a string

data s;
name='ram28062018kumar' ;
output ;
name='venkat15072018sharma' ;
output;
run;

/*this my code to extract first name ,last name,dob.

is there any other coding?*/

 

data s1 ;
set s ;
finame=substr(name,1,anydigit(name)-1) ;
laname=reverse(substr(reverse(name),1,anydigit(reverse(name))-1));
dob1=compress(name,' ','kd') ;
dob= input(compress(substr(dob1,1,2)||'-'||substr(dob1,3,2)||'-'||substr(dob1,5,4)),ddmmyy10.);
format dob ddmmyyd10. ;
drop dob1;
run;

 

PROC Star
Posts: 1,410

Re: how to derive first name ,lastname,dob from a string

Posted in reply to thanikondharish

What do you mean by "is there any other coding?"?

 

Your code does what it is suppose to, it seems?

Frequent Contributor
Posts: 88

Re: how to derive first name ,lastname,dob from a string

i mean any other short way
Super User
Posts: 10,623

Re: how to derive first name ,lastname,dob from a string

Posted in reply to thanikondharish

You don't need to insert hyphens in the date string; just use the ddmmyy8. informat.

Otherwise, your code looks fine.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,939

Re: how to derive first name ,lastname,dob from a string

Posted in reply to thanikondharish

You don't really need to create dob1:

 

dob = input(compress(name,,'kd'), ddmmyy8.);

Trusted Advisor
Posts: 1,400

Re: how to derive first name ,lastname,dob from a string

Posted in reply to thanikondharish

You've already have good advice on getting dob.  Here's a simplication for first and last names:

 

  fname=scan(name,1,'0123456789');
  lname=scan(name,-1,'0123456789');

 

The "-1" argument for scan in the lname assignment says to count backwards from the right-most word.  This mean you don't have to worry about how many digits are in the dob value.

Ask a Question
Discussion stats
  • 5 replies
  • 81 views
  • 0 likes
  • 5 in conversation