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

hi, I have a date 05/13 in a variable named 'date'.

1) now i want to split, date and month. How to split the date as day and month.

2) its is also in character format. how do i convert it to numeric..

 

                            date  

                         05/13.

 

i want this as     day   month

                        13           05          

 
 
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

SCAN() will allow you to separate the components based on a delimiter, in this case your /.
INPUT() will convert the values to numeric.

Another approach is to read it in as a date and then use DAY() and MONTH() functions.

 

data example;
	have='05/13';
	
	*character extraction;
	month_char=scan(have, 1, '/');
	day_char=scan(have, 2, '/');
	
	*conversion to numeric;
	month_num=input(month_char, 8.);
	day_char=input(day_char, 8.);
	
	*same as above but single step;
	month_num2=input(scan(have, 1, '/'), 8.);
	day_num2=input(scan(have, 2, '/'), 8.);
	
	*date method - requires year or custom informat;
	have_date=input(catt(have, '/2020'), mmddyy10.);
	month_num3=month(have_date);
	day_num3=day(have_date);
run;

 

View solution in original post

7 REPLIES 7
Reeza
Super User

SCAN() will allow you to separate the components based on a delimiter, in this case your /.
INPUT() will convert the values to numeric.

Another approach is to read it in as a date and then use DAY() and MONTH() functions.

 

data example;
	have='05/13';
	
	*character extraction;
	month_char=scan(have, 1, '/');
	day_char=scan(have, 2, '/');
	
	*conversion to numeric;
	month_num=input(month_char, 8.);
	day_char=input(day_char, 8.);
	
	*same as above but single step;
	month_num2=input(scan(have, 1, '/'), 8.);
	day_num2=input(scan(have, 2, '/'), 8.);
	
	*date method - requires year or custom informat;
	have_date=input(catt(have, '/2020'), mmddyy10.);
	month_num3=month(have_date);
	day_num3=day(have_date);
run;

 

Raj00007
Calcite | Level 5
i tried using day and month functions, but it is showing error. can you please write the code here
Reeza
Super User
Show your code please. You cannot use MONTH/DAY() on a character variable so you first need to convert it.
Raj00007
Calcite | Level 5
data springrain;
set lib.SPRING_RAIN;
num = input (date, mmdd5.);
day = day (num);
month = month (num);
run;
proc print;
run;


here the informat mmdd5. says invalid. what is the right informat for 05/13 . and also day and month functions are no working.
Reeza
Super User
There is no informat for just mmdd so you need to either add the year or create a custom informat.
ballardw
Super User

Since a SAS date value must have a year there cannot be an informat to create a date from something without a year value.

 

Your best bet is likely to parse the character value and create numeric values of Month and Day and then create a date with the MDY function where you supply the year value.

PaigeMiller
Diamond | Level 26
month=scan(date,1,'/');

day=scan(date,2,'/');
--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2036 views
  • 0 likes
  • 4 in conversation