BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bluekeys49
Obsidian | Level 7
I have a variable SCH_DATE in a data set that is currently numeric with default 8 length. Some of the data reflected in this field looks like the following in month, day, year order:

120968
52192
101481
91565

Ultimately, I want the SCH_DATE to look as follows, to include leading 0s:

120968
052192
101481
091565

How do I make the conversion?
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

The internal storage of a value in a variable of type Numeric will never have leading zero's - but you can always attach a format permanently to the variable as shown above.

The other option is to save the digits as a string in a new variable of type Character as shown below:

sch_date_c=put(SCH_DATE, z6.);

 

View solution in original post

12 REPLIES 12
Patrick
Opal | Level 21

If this is just about how a numerical value displays/prints then you can use format Zw.d

format SCH_DATE z6.;

 

Bluekeys49
Obsidian | Level 7
Thank you. I also need it stored permanently as the new value with leading 0s in another SAS data set.
Patrick
Opal | Level 21

The internal storage of a value in a variable of type Numeric will never have leading zero's - but you can always attach a format permanently to the variable as shown above.

The other option is to save the digits as a string in a new variable of type Character as shown below:

sch_date_c=put(SCH_DATE, z6.);

 

Patrick
Opal | Level 21

@Bluekeys49 

I believe you should go for the approach @Kurt_Bremser proposed and convert your data to a SAS Date value as this will allow you to actually work with this data as dates - like calculating the number of days between two dates.

Kurt_Bremser
Super User

When values reflect dates, they have to be stored as dates:

length sch_date_new 4;
sch_date_new = input(put(sch_date,z6.),mmddyy6.);
format sch_date_new yymmddd10.;

Because of the sheer stupidity of still using 2-digit years after Y2K, you will have to adjust the YEARCUTOFF option value.

Patrick
Opal | Level 21

@Bluekeys49 

I've totally missed that these values represent dates. Definitely use what @Kurt_Bremser proposes and convert these numbers into SAS Date values. This then allows you to actually use SAS calendar functions like intnx() and intck().

Bluekeys49
Obsidian | Level 7

Thank you!  More than one of the options worked.  Appreciate the help!

ChrisNZ
Tourmaline | Level 20

You don't have to use the length statement, especially if your data set is compressed, but @Kurt_Bremser reply is the only way to go: store a date as a date.

Kurt_Bremser
Super User

I prefer to use the length statement because compressing a lot of essentially nothing (hex 00's) still needs more space than not having those bytes in the first place. It's not much, but this old coder hates wasting space and CPU cycles.

ChrisNZ
Tourmaline | Level 20

@Kurt_Bremser I used to do this, but most users would not do it, and I got tired of length-mismatch messages for a small benefit as long as the table is compressed. No right or wrong of course.

Kurt_Bremser
Super User

I do have the benefit of a tightly knitted environment when it comes to data sources. 99% originates from DB/2 or SAP, and is imported by centrally controlled (read: me and two others <g>) processes/programs. We have a macro defined for importing columns with types (char/date/datetime/time/packed decimal/zoned/UUID, even binary), so the length of a date is actually set in one include for all data we have.

Only when end users import their own data, they can have length issues, but that happens usually only once per user, then they know.

ChrisNZ
Tourmaline | Level 20
It sounds like you have a much tighter environment than where I work. Congratulations:)

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 762 views
  • 5 likes
  • 4 in conversation