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

Dear all,

I am new to SAS have a question regarding the processing of date time in SAS. My data variable is formatted as follows:

1920/11/1 0:00	
1920/11/21 0:00
1920/5/15 0:00
1920/8/27 0:00	
1921/7/14 0:00
1921/7/26 0:00	
1921/8/2 0:00	
1922/2/12 0:00	
1922/2/22 0:00	
1923/11/1 0:00	
1923/12/3 0:00	
1923/2/13 0:00

So they are of irregular lengths and as I understand they do not fit any of the SAS built-in format. They do not have "0"s before or behind the single-digit months or dates.

The result I am expecting is to 1) get rid of the last 5 digits of the clock time; 2) transform the date variable into standard format and then SAS date values (days comparing with Jan/01/1960) and 3) generate 2 variables that display the month and day of the date variable in readable format (but still calculatable).

Example:

Original date variable: 1921/7/14 0:00

Converted into 1921/07/14 which is about negative sth SAS date value

monthvar: 1921/07

datevar: 1921/07/21

 

Not really sure how to achieve that, especially with the irregular format. Please help! Thanks a lot!

 

Ginny Han

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Are you asking how to change your existing strings into date values?  If so you are looking for an INFORMAT, not a format.  Since you don't care about the time of day part just ignore it.  Once you have a date value you can use any of the many SAS formats that display dates. Like YYMMDD.

data want ;
  set have ;
  date=input(scan(date_string,1,' '),yymmdd10.);
  format date yymmdd10. ;
run;
Obs      date_string            date

  1    1920/11/1 0:00     1920-11-01
  2    1920/11/21 0:00    1920-11-21
  3    1920/5/15 0:00     1920-05-15
  4    1920/8/27 0:00     1920-08-27
  5    1921/7/14 0:00     1921-07-14
  6    1921/7/26 0:00     1921-07-26
  7    1921/8/2 0:00      1921-08-02
  8    1922/2/12 0:00     1922-02-12
  9    1922/2/22 0:00     1922-02-22
 10    1923/11/1 0:00     1923-11-01
 11    1923/12/3 0:00     1923-12-03
 12    1923/2/13 0:00     1923-02-13

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Are you asking how to change your existing strings into date values?  If so you are looking for an INFORMAT, not a format.  Since you don't care about the time of day part just ignore it.  Once you have a date value you can use any of the many SAS formats that display dates. Like YYMMDD.

data want ;
  set have ;
  date=input(scan(date_string,1,' '),yymmdd10.);
  format date yymmdd10. ;
run;
Obs      date_string            date

  1    1920/11/1 0:00     1920-11-01
  2    1920/11/21 0:00    1920-11-21
  3    1920/5/15 0:00     1920-05-15
  4    1920/8/27 0:00     1920-08-27
  5    1921/7/14 0:00     1921-07-14
  6    1921/7/26 0:00     1921-07-26
  7    1921/8/2 0:00      1921-08-02
  8    1922/2/12 0:00     1922-02-12
  9    1922/2/22 0:00     1922-02-22
 10    1923/11/1 0:00     1923-11-01
 11    1923/12/3 0:00     1923-12-03
 12    1923/2/13 0:00     1923-02-13
Ginny_Han
Calcite | Level 5

Hi Tom,

Thank you very much for your swift solution. It worked perfectly. Never considered the possibility of using the -scan- function. Thought I should start with eliminating some digits from the end.

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

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
  • 2 replies
  • 797 views
  • 0 likes
  • 2 in conversation