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
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;
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;
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.
month=scan(date,1,'/');
day=scan(date,2,'/');
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!
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.