Misha
Misha

Reputation: 3126

convert string to date in sas

I have a dataset in SAS that where I need to convert one string to date and do a subtraction before importing it into R. Problem is that I know R, but do not have the slightest clue about SAS.

The string is "06117344370" where I need to substring the first 6 characters (261173) and convert them into a date. This is how far I´ve come:

(SUBSTR(string,1,6) as Bday

I guess I could use INPUT before and turn it into a number, but then I´m left with "61173" rather than 061173, and parsing that to a date function is probably difficult.

Upvotes: 0

Views: 67

Answers (1)

SMW
SMW

Reputation: 502

Your close. First step would be to convert your target string to limit the field to your required characters that represent your date:

string_date = substr(string,1,6);

Then you can take your substring character field string_date and apply an input statement:

convert = input(string_date,mmddyy6.);

What you will be left with is a number that you will not recognize as a date. This is where you want to apply your format so its an actual "date", not just a string that represents a date:

format convert date9.;

You can run the full data step below so you can see how the process works:

data have;
string = '06117344370';
string_date = substr(string,1,6);
convert = input(string_date,mmddyy6.);
format convert date9.;
run;

Upvotes: 1

Related Questions