BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ilikesas
Barite | Level 11

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
Reeza
Super User

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;

ilikesas
Barite | Level 11

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 

Reeza
Super User

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

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

OleSteen
SAS Employee

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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