SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to select a date from a string conditional on a part of that string

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

How to select a date from a string conditional on a part of that string

Hi,

suppose I have the following table:

Company
ABC (merged 1/10/2012)
DEF (bankrupt 2/5/2009)
ZZZ

If the company merged, I would like to get a new column for the date when it happened, and nothing for the others, in the following way:

CompanyMerged
ABC (merged 1/10/2012)1/10/2012
DEF (bankrupt 2/5/2009)
ZZZ

Thank you


Accepted Solutions
Solution
‎01-03-2015 11:24 PM
Super User
Posts: 17,819

Re: How to select a date from a string conditional on a part of that string

You're previous questions should allow you to isolate which variables have the word merged.

Adding on to that, you can use the scan function to get the date, and in conjunction with the input function convert it to a valid SAS date which is highly recommended. I'm assuming the format of the dates is mmddyy though it's unclear based on your date format.

data have;

length var $30.;

var="ABC (merged 1/10/2012)";output;

var="DEF (bankrupt 2/5/2009)";output;

var="ZZZ";output;

run;

data want;

set have;

date=scan(var, -1, "() ");

sas_date=input(scan(var, -1, "() "), mmddyy10.);

format sas_date date9.;

run;

View solution in original post


All Replies
Solution
‎01-03-2015 11:24 PM
Super User
Posts: 17,819

Re: How to select a date from a string conditional on a part of that string

You're previous questions should allow you to isolate which variables have the word merged.

Adding on to that, you can use the scan function to get the date, and in conjunction with the input function convert it to a valid SAS date which is highly recommended. I'm assuming the format of the dates is mmddyy though it's unclear based on your date format.

data have;

length var $30.;

var="ABC (merged 1/10/2012)";output;

var="DEF (bankrupt 2/5/2009)";output;

var="ZZZ";output;

run;

data want;

set have;

date=scan(var, -1, "() ");

sas_date=input(scan(var, -1, "() "), mmddyy10.);

format sas_date date9.;

run;

Super Contributor
Posts: 413

Re: How to select a date from a string conditional on a part of that string

Hi Reeza,

thank you for replying and for reading my previous question!!!

I would like to ask a question, suppose I had ABC ( merged in 1/10/2012 because of reasons), when applying the scan function the "date" that I will get is "reasons" because its the last word, so is there a way of getting the date regardless of its position in the string?

Thank you 

Super User
Posts: 17,819

Re: How to select a date from a string conditional on a part of that string

Any other variations? Will there be other numbers or slashes in the text?

You can look into the index/find/substr functions.

SAS Employee
Posts: 19

Re: How to select a date from a string conditional on a part of that string

I would go for Perl regular expression functions: E.g. PRXCHANGE, PRXMATCH etc.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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