If your dates are text strings, it is much harder to do. If your dates are valid SAS date values, its much easier.
But its really not clear to me what you are trying to do, or what input data you have. Do you really have '2019-01-JAN' to indicate January, 2019 where both the '01' and 'JAN' always go together redundantly? Would you ever get '2019-02-JAN' and what would you do with it, what value of Date2 would you want?
And the input data always has dates as text strings in this unusual format, or is that something you have created, which could be simplified?
@Son_Sam wrote:
The both the dates are strings
And we want it in string format
What if convert them into date format and again in string
Do you want to write on clay tablets? Calculate by counting pebbles?
What is the specific logic behind "we want it in string format"? What specific task is made easier by having the value in a string?
SAS has quite a number of formats for displaying date values in different ways and you can build your own. The first time you need to do almost anything involving the value, such as "beginning of the month" (HINT) then an actual date value makes much more sense.
I don't think you have clearly answered most of my questions. Is the '01' and 'Jan' redundant? What happens if you get '02' and 'Jan'?
Are you simply trying to remove "Jan" and "Mar" from the string?
Can you cut and paste in the actual code? The code as is cannot be actual code because it has errors.
It is very helpful if you post:
Jim
If you had SAS dates instead of these silly strings, it would be extremely simple (just a change of format). Storing date values like this is, to be polite, utter stupidity.
So the first thing you do is fix the process that creates these strings.
If the format of your strings is *very* consistent, then you could just do the following:
DATA M1_000;
INFILE DATALINES4;
LENGTH Date1 $11;
INPUT Date1 $;
DATALINES4;
2019-01-Jan
2019-02-Feb
2019-03-Mar
2019-04-Apr
2019-12-Dec
2020-01-Jan
2020-02-Feb
2020-03-Mar
2020-04-Apr
2020-12-Dec
;;;;
RUN;
DATA M_000;
SET M1_000;
LENGTH Date2 $7;
Date2 = SUBSTR(STRIP(Date1), 1, 7);
RUN;
Results:
Jim
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.