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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1064 views
  • 6 likes
  • 3 in conversation