Help using Base SAS procedures

Retaining leading zero

Reply
N/A
Posts: 0

Retaining leading zero

Hello, I'm trying to create a date stamp in SAS from an inputted variable, but I'm having a problem when truncating year from 4 digits to 2 digits. SAS is dropping the leading zero.

For example;

month = 01
day = 15
year = 2009

Trying to create the date stamp it currently appears as 1159 instead of 011509 (which is what is needed).

If I need to supply more information, please let me know. Any help would be greatly appreciated.
Super Contributor
Super Contributor
Posts: 3,174

Re: Retaining leading zero

Posted in reply to deleted_user
Suggest you share the code that generates the SAS variable - the reason is because you have not identified what output format you are using, nor whether you are already converting a SAS "numeric type" DATE variable to character, by using a PUT function.

Suggest also having a look at the PROC FORMAT documentation and particularly the use of a "custom date format" with PICTURE.

Scott Barry
SBBWorks, Inc.

SAS Procedures Guide, PICTURE Statement
http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a002473467.htm
Super Contributor
Posts: 474

Re: Retaining leading zero

Posted in reply to deleted_user
You are probably converting numerics to alphas without specifying a leading zero format.
[pre]
data _null_;

DT='15JAN2009'd;

* explicit conversion, no leading zero format;
M=put(month(DT),2.);
D=put(day(DT),2.);
Y=put(mod(year(DT),100),2.);
X=cats(M,D,Y);
put _all_; * wrong;

* implicit conversion, no format at all;
M=month(DT);
D=day(DT);
Y=mod(year(DT),100);
X=cats(M,D,Y);
put _all_; * wrong;

* explicit conversion, with leading zero format;
M=put(month(DT),z2.);
D=put(day(DT),z2.);
Y=put(mod(year(DT),100),z2.);
X=cats(M,D,Y);
put _all_; * right!;

run;
[/pre]
But, that's only an assumption.

Help us to help you. As said above, you should always share you're code.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt
Respected Advisor
Posts: 3,799

Re: Retaining leading zero

Posted in reply to deleted_user
"always" convert date data to SAS/Date. Then you can display with any format. The one you want is built in.
[pre]
359 data _null_;
360
361 month = 01;
362 day = 15;
363 year = 2009;
364
365 date = mdy(month,day,year);
366
367 put 'STAMP: ' date mmddyyN6.;
368 run;

STAMP: 011509
[/pre] Message was edited by: data _null_;
Ask a Question
Discussion stats
  • 3 replies
  • 112 views
  • 0 likes
  • 4 in conversation