BookmarkSubscribeRSS Feed
Son_Sam
Calcite | Level 5
Hello Everyone,
I want to automate the sas if else statement at the beginning of each month I have to write if else statement code below:
Where 2019 is year 01 is month number and Jan is month name in date1
Data m_000;
Set m1_000;
If date1='2019-01-Jan then Date2='2019-1';
If date1='2019-03-Mar then Date2='2019-03;
Run;

And
Every time I have to write the code add new month I don't want to add new month every time could this can be automated and how please advice.
17 REPLIES 17
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
Son_Sam
Calcite | Level 5
The both the dates are strings
And we want it in string format
What if convert them into date format and again in string
ballardw
Super User

@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.

Son_Sam
Calcite | Level 5
Yes we can do that and agai. Convert them into the string
The logic behind is we don't want to add month name string each month
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
jimbarbour
Meteorite | Level 14

Can you cut and paste in the actual code?  The code as is cannot be actual code because it has errors. 

jimbarbour_0-1600714451208.png

 

It is very helpful if you post:

  1. Actual code
  2. The log statements if any errors or warnings are issues
  3. The results.

Jim

Son_Sam
Calcite | Level 5
Data v1;
Set V2;
If date1='2019-01-jan' then Date2='2019-01' else
If date1='2019-02-feb' then Date2='2019-02' else
If date1='2019-03-mar' then Date2='2019-03'
Run;
Where both date1 and Date2 are character.
Kurt_Bremser
Super User

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.

Son_Sam
Calcite | Level 5
Can you please provide me the code
jimbarbour
Meteorite | Level 14

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:

jimbarbour_0-1600715213179.png

 

Jim

 

Reeza
Super User
It looks like you're just taking the first 7 characters of the string. Why bother with an IF/ELSE at all?

date2 = substr(date1, 1, 7);

Son_Sam
Calcite | Level 5
Can we do it for each date1 and how
Reeza
Super User
Did you try it?
Son_Sam
Calcite | Level 5
Yes this for Date2 only and we don't want to add date1 every month by own

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 Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 17 replies
  • 743 views
  • 2 likes
  • 6 in conversation