BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello,
I have really messy data where I have a date variable and it looks like the following:

2/5/04
11/25/06
5/17/03
...etc.

I'd like to make it consistent by adding '0's in front of the single digits..so I'd like to make it look like:

02/05/04
...
05/17/03
...etc.

How do I do this in SAS?

Thanks!!!
1 REPLY 1
Cynthia_sas
SAS Super FREQ
Hi:
Is your date variable a character variable or a numeric variable?

The method you use to show leading zeroes in the month or the year will depend on whether you're dealing with a character variable value or a numeric variable value.

Let's assume that your date variable is a numeric variable -- that means that SAS stores the first value not as "2/5/04" but as 16106 -- which represents the number of days between Feb 5, 2004 and Jan 1, 1960 (the SAS zero date). In this instance, a simple FORMAT statement in your PROC PRINT or your procedure would display the date variable's values with leading zeroes.

On the other hand, if your variable is a character variable, then a FORMAT statement will not work to alter the display of your data values. By far, I think the FORMAT statement method -- is the EASIEST way to handle the display of date variable values. But, that means the you must transform your character date variable into a numeric variable -- that is internally stored as the number of days either before or after Jan 1, 1960. If you are READING raw data into SAS format, then when you read '2/5/04', it is possible, on your INPUT statement, to tell SAS to read the messy number and turn it into a numeric date variable. If you already have messy data and the variable is a character variable, then you can transform it with the INPUT function.

See the program below for an example of both the INPUT statement and the INPUT function.

cynthia
[pre]
** Program;

data messy;
infile datalines;
input datevar : mmddyy8. chardate $8.;
return;
datalines;
2/5/04 2/5/04
11/25/06 11/25/06
5/17/03 5/17/03
11/15/50 11/15/50
;
run;

proc contents data=messy;
title '1) What are my variables like';
run;

proc print data=messy;
title '2) No format -- see internal value';
run;

proc print data=messy;
title '3) MMDDYY8 format';
format datevar mmddyy8.;
run;

data fixdate;
set messy;
newdate = input(chardate,mmddyy8.);
run;

proc contents data=fixdate;
title '4) Is NEWDATE variable character or numeric';
run;

proc print data=fixdate;
title '5) CHARDATE variable turned into numeric variable NEWDATE';
format datevar newdate mmddyy8.;
run;

[/pre]

Output:
[pre]
1) What are my variables like

The CONTENTS Procedure

Data Set Name WORK.MESSY Observations 4
Member Type DATA Variables 2
Engine V9 Indexes 0
Created Thursday, March 20, 2008 11:22:45 AM Observation Length 16
Last Modified Thursday, March 20, 2008 11:22:45 AM Deleted Observations 0
Protection Compressed NO
Data Set Type Sorted NO
Label
Data Representation WINDOWS_32
Encoding wlatin1 Western (Windows)


Engine/Host Dependent Information

Data Set Page Size 4096
Number of Data Set Pages 1
First Data Page 1
Max Obs per Page 252
Obs in First Data Page 4
Number of Data Set Repairs 0
File Name C:\DOCUME~1\sasczz\LOCALS~1\Temp\SAS Temporary Files\_TD2592\messy.sas7bdat
Release Created 9.0101M3
Host Created XP_PRO


Alphabetic List of Variables and Attributes

# Variable Type Len

2 chardate Char 8
1 datevar Num 8

***************************************************

2) No format -- see internal value

Obs datevar chardate

1 16106 2/5/04
2 17130 11/25/06
3 15842 5/17/03
4 -3334 11/15/50

***************************************************

3) MMDDYY8 format

Obs datevar chardate

1 02/05/04 2/5/04
2 11/25/06 11/25/06
3 05/17/03 5/17/03
4 11/15/50 11/15/50

***************************************************

4) Is NEWDATE variable character or numeric

The CONTENTS Procedure

Data Set Name WORK.FIXDATE Observations 4
Member Type DATA Variables 3
Engine V9 Indexes 0
Created Thursday, March 20, 2008 11:22:45 AM Observation Length 24
Last Modified Thursday, March 20, 2008 11:22:45 AM Deleted Observations 0
Protection Compressed NO
Data Set Type Sorted NO
Label
Data Representation WINDOWS_32
Encoding wlatin1 Western (Windows)


Engine/Host Dependent Information

Data Set Page Size 4096
Number of Data Set Pages 1
First Data Page 1
Max Obs per Page 168
Obs in First Data Page 4
Number of Data Set Repairs 0
File Name C:\DOCUME~1\sasczz\LOCALS~1\Temp\SAS Temporary Files\_TD2592\fixdate.sas7bdat
Release Created 9.0101M3
Host Created XP_PRO


Alphabetic List of Variables and Attributes

# Variable Type Len

2 chardate Char 8
1 datevar Num 8
3 newdate Num 8

***************************************************

5) CHARDATE variable turned into numeric variable NEWDATE

Obs datevar chardate newdate

1 02/05/04 2/5/04 02/05/04
2 11/25/06 11/25/06 11/25/06
3 05/17/03 5/17/03 05/17/03
4 11/15/50 11/15/50 11/15/50
[/pre]

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 1 reply
  • 3674 views
  • 0 likes
  • 2 in conversation