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:
Company | Merged |
---|---|
ABC (merged 1/10/2012) | 1/10/2012 |
DEF (bankrupt 2/5/2009) | |
ZZZ |
Thank you
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;
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;
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
Any other variations? Will there be other numbers or slashes in the text?
You can look into the index/find/substr functions.
I would go for Perl regular expression functions: E.g. PRXCHANGE, PRXMATCH etc.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.