BookmarkSubscribeRSS Feed
sks521
Quartz | Level 8

Hi folks,

 

I have a date variable with two different date formats; like 201703 and 20170319.

 

How do I re-code to generate a single format please?

 

Ta

5 REPLIES 5
Satish_Parida
Lapis Lazuli | Level 10
Where the data is stored, is it in a SAS dataset or any other format e.g. Excel, CSV.
If it is in SAS Dataset, Is the variable a Character or a Numeric Data Variable?
sks521
Quartz | Level 8

Hi,

 

The data is stored in a CSV

 

Thanks

Satish_Parida
Lapis Lazuli | Level 10

I would suggest to use a data step to read the CSV file.
1. Then read the particular variable as a character.
2. add some default date when the date field is absent.
3. Convert the char date to number date.

 

Example code snippet.

 

data have;
input f_date:$8.;

if length(f_date)<8 then t_date=cats(f_date,'01'); /*01 is some random number*/
else t_date=f_date;

a_date=input(t_date,yymmdd8.);

cards;
201703
20170319
;
run;
sks521
Quartz | Level 8

Hi Satish,

 

I have tried to run the code as you have suggested but I think I want to do it differently. My Date of Birth variable has values like '201702' and some values are missing.

 

And Attendance_date has two different formats like; 201903 and 20150623.

 

I have read the two fields as character $8.

 

I want to do following;

 

Derive the age of participants from Dob and attendance_date. Please suggest the best possible way.

 

Thanks

ballardw
Super User

@sks521 wrote:

Hi Satish,

 

I have tried to run the code as you have suggested but I think I want to do it differently. My Date of Birth variable has values like '201702' and some values are missing.

 

And Attendance_date has two different formats like; 201903 and 20150623.

 

I have read the two fields as character $8.

 

I want to do following;

 

Derive the age of participants from Dob and attendance_date. Please suggest the best possible way.

 

Thanks


Please provide explicit examples of your input and the desired actual date you want them treated as.

And do you want Age in years as commonly done or year and fractional part or some other unit?

The first step is going to settle on a base rule for the incomplete dates. So specify if that 201903 is supposed to be the first day of March in 2019, another day in March in 2019, or something else. Note that 6 character dates can have lots of interpretations and you need to explicitly tell us what you want to use.

 

Example: 010203 could be 02JAN2003 , 03Feb2001, 01Feb2003  or the years might be 1903, 1901 or 1903. Any 2 digit year is hightly suspect in my book.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1041 views
  • 0 likes
  • 3 in conversation