All,
I know there are many posts on the community and I tried following the suggestions in these posts without much success. Here is a very relevant post (https://communities.sas.com/t5/SAS-Programming/URGENT-extract-Year-and-Month-from-a-date-variable/td...) .
Let me show what I have tried to do and my code:
Data Have; Set Work.temp (Keep = UtcReadTime ); Left = scan(UtcReadTime,1,'T'); Right = scan(UtcReadTime,2,'T'); format Left yymmdd8.; format Right yymmdd8.; Date = input(Left,yymmdd8.); Year = Year(Date); Month = Month(Date); Day = Day(Date); Hour = Hour(Right); Run;
The only problem with your code was the width of the informat. By asking yymmdd8. you were missing the last two characters of the date part of the string. Look:
218 data _null_; 219 str = "2019-02-27T04:00:00"; 220 date = input(str, yymmdd10.); 221 format date yymmddn8.; 222 put (_all_) (=); 223 run; str=2019-02-27T04:00:00 date=20190227
This seems to work, but I am curious as to why my previous approach does not work :
Data Have; Set Work.sitepvforecast (Keep = UtcReadTime ); Left = scan(UtcReadTime,1,'T'); Right = scan(UtcReadTime,2,'T'); Year = substr(Left,1,4); Year_n = input(Year, best12.); Month = substr(Left,6,2); Month_n = input(Month,best12.); Day = substr(Left,9,2); Day_n = input(Day,best12.); Hour = substr(Right,1,2); Hour_n = input(Hour,best12.); Run;
SAS functions YEAR, MONTH, DAY, etc. work on SAS dates which are numbers. You were applying them to character strings.
204 data _null_; 205 t = today(); 206 y = year(t); 207 m = month(t); 208 d = day(t); 209 str = put(t, yymmdd10.); 210 put (_all_) (=); 211 run; t=21606 y=2019 m=2 d=26 str=2019-02-26
@PGStats ,
Thank you for your response. If you look at the link that I posted, the solution achieved in that discussion also goes through a similar set of steps.
If your source variable is CHARACTER then you need to make a NEW variable if want to store it as an actual date value.
data want ;
set have ;
first_chemo_date_num=input(first_chemo_date,yymmdd8.);
format first_chemo_date_num yymmddn8.;
yr=year(first_chemo_date_num);
mth=month(first_chemo_date_num);
run;
I understand that I am starting of with a character, however I thought the input function was supposed to cast it into a numeric date as explained in this example and that is what I tried. Just to be clear, besides the example code I posted, I also tried creating a new variable exactly as shown .
The only problem with your code was the width of the informat. By asking yymmdd8. you were missing the last two characters of the date part of the string. Look:
218 data _null_; 219 str = "2019-02-27T04:00:00"; 220 date = input(str, yymmdd10.); 221 format date yymmddn8.; 222 put (_all_) (=); 223 run; str=2019-02-27T04:00:00 date=20190227
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.