BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Charleswong001
Fluorite | Level 6

How do I change the date from e.g. 2-Nov-07 to 02-Nov-1907 so that all the dates in the column can be in the new format (dd-mmm-yyyy)

I have a rather big data set and there are a lot on dates similar to the one above I need to change

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

For these two formats, the DATE11 informat should work:

birthday_num = input(birthday,date11.);
format birthday_num yymmdd10.;

Use any date format to your liking in the FORMAT statement. 

View solution in original post

13 REPLIES 13
Kurt_Bremser
Super User

A SAS date is a number counting the days from 1960-01-01, and how it is displayed is controlled by the format used. So, if your variable is a SAS date and not a character variable, all you need to do is change the format with PROC DATASETS, which will only change the dataset header.

Inspect your variable(s) either by right-clicking on the column in a table viewer, or run PROC CONTENTS.

Charleswong001
Fluorite | Level 6
Sorry I still don't quite understand, here's my question, hope it clarifies more than what I mentioned above.

(Since some of the birth years are recorded in the two-digit format (e.g., “81” for “1981”, “07” for “2007”, etc.), we need to add the prefix (e.g., “19”) to those values. Furthermore, we need to standardise the day of birth to the two-digit format as well (e.g. “02-Nov-2004” instead of “2-Nov-2004”). Implement a new DATA step to modify the variables “Day” and “Year”)
andreas_lds
Jade | Level 19

Please read the docs about dates in sas: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/n0c9zxlm4e6m7tn1vrn76c98zw66.ht...

 

If your dates are stored a strings, fixing this will automatically fix your issue. You need to show an excerpt of the data, if you need more assistance.

Kurt_Bremser
Super User

It seems(!) you have dates stored as character, which is sub-optimal, to say the least.

If this is really the case (run PROC CONTENTS!), we need to see comprehensive examples of the different styles, so we can suggest how to make usable SAS dates out of the character data.

Charleswong001
Fluorite | Level 6

My birthday was not set as a string

Ran the proc contents, attached an image below

proc contents gba.png

Kurt_Bremser
Super User

If birthday is the variable we are talking about, then it is character (see the list).

For the other dates (date, anniv, prevdate), they are already SAS dates, so only a change of format is needed to change how they are displayed.

So what is it that you need to do?

Charleswong001
Fluorite | Level 6

I need to change dates inside the columns that are like this "2-oct-07" to this 02-oct-1907

Charleswong001
Fluorite | Level 6

The birthday column, some are in the format I want which is -> 02-Oct-1987 but some are 2-oct-07. 

Sorry, just started SAS and I'm not good in all these

Kurt_Bremser
Super User

For these two formats, the DATE11 informat should work:

birthday_num = input(birthday,date11.);
format birthday_num yymmdd10.;

Use any date format to your liking in the FORMAT statement. 

Tom
Super User Tom
Super User

Welcome to the Y2K problem.

 

Read the field as character and add your own logic to convert it to dates.

You can check for the values that only have years.  You could just give set those to Jan first.

 

Then compare the birthyear to the award year and when they won before they were born adjust the dob by 100 years.

filename csv 'c:\downloads\oscar.csv';
data want;
  infile csv firstobs=2 dsd truncover ;
  length ID $20 Birthplace $40 Birthday $11 AwardYear 8 Award $30 biourl $200 Movie $100 Person $50 ;
  input id -- person;
  if length(birthday)=4 then dob=mdy(1,1,input(birthday,4.));
  else dob = input(birthday,date11.);
  format dob date9.;
  if AwardYear - year(dob) < 0 then dob=intnx('year',dob,-100,'s');
  age = AwardYear - year(dob);
run;

proc print data=want(drop=biourl);
run;
Reeza
Super User

How do you know if 26-AUG-22 is 1922 or 2022?

Charleswong001
Fluorite | Level 6

@Tom @Kurt_Bremser 😄Thanks everyone! Problem solved!

@Reeza my prof gave a hint haha

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 13 replies
  • 6128 views
  • 10 likes
  • 5 in conversation