- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
(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”)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
My birthday was not set as a string
Ran the proc contents, attached an image below
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I need to change dates inside the columns that are like this "2-oct-07" to this 02-oct-1907
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
WHICH COLUMNS???
Name them!!! And show values which need to be corrected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
How do you know if 26-AUG-22 is 1922 or 2022?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Tom @Kurt_Bremser 😄Thanks everyone! Problem solved!
@Reeza my prof gave a hint haha