Help using Base SAS procedures

Adding leading zeros to numeric fields

Reply
Frequent Contributor
Posts: 120

Adding leading zeros to numeric fields

HHi,

hi hi I want to add leading zeros to numeric fields and the docurgent the format is I3

tthat is Integer 3 it means when age is 36 we don't need to add leading zeros because 36 is 2 bytes and +sign occupies 1byte

but when age is 8 then add one leading zero . Can anybody suggest

Super User
Posts: 5,083

Re: Adding leading zeros to numeric fields

Sounds like you need a PICTURE format:

proc format;

   picture this low-high='99'  (prefix='+');

run;

Then when printing a variable, add:

format varname this3.;

Good luck.

Frequent Contributor
Posts: 120

Re: Adding leading zeros to numeric fields

HHi

i have used  Newage= put(input(old_age,best2.),z2.); I gave me leading zeros

Old age

36

2

45

6

new age

36

02

45

06

but when I am exporting data to csv file I am missing leading zeros

data in Csv file

36

2

45

6

can any one pls help me .

Super User
Super User
Posts: 6,500

Re: Adding leading zeros to numeric fields

The zeros are in the CSV file, but you are probably opening the file in Excel and it is converting them back to a number.

Open the CSV file with a text editor and you will see the zeros.

Frequent Contributor
Posts: 120

Re: Adding leading zeros to numeric fields

No I am not seeing it . One of my collague is suggesting that everything in Csv shd be strings

so I don't understand what to do

Respected Advisor
Posts: 3,892

Re: Adding leading zeros to numeric fields

Open your .csv file with a text editor like Notepad and check if you can see the zeros this way. If so then Tom's assumption is confirmed and we can discuss of how to write strings of digits to a .csv file so that when opening it in Excel digit strings don't get converted into numbers.

Frequent Contributor
Posts: 120

Re: Adding leading zeros to numeric fields

I Opened it in text editor I don't find any leading zeros

Regular Contributor
Posts: 180

Re: Adding leading zeros to numeric fields

You have to be sure that Newage is a character variable with length=3, not a numeric one. Show your code.

CTorres

Super User
Posts: 10,500

Re: Adding leading zeros to numeric fields

If you did a SAVE in Excel it corrupted the data.

Respected Advisor
Posts: 3,892

Re: Adding leading zeros to numeric fields

Just for having leading zero's for numeric variables you can use the out-of-the-box Zw.d format for this.  SAS(R) 9.4 Formats and Informats: Reference

I'm not really sure what you're trying to explain with the bytes. I would have thought that for numeric variables the +/- consumes one bit only (and not a byte).

If you're dealing with character variables then please provide some representative sample data and then explain how the result should look like.

Frequent Contributor
Posts: 120

Re: Adding leading zeros to numeric fields

FFirst age variable in dataset is numeric with length 8

then I changed like this

data x (drop= age);

set y;

old_age= put( age,2.);

run;


then in another data step i converted to


i have used  Newage= put(input(old_age,best2.),z2.); I

whren I am exporting to Csv.    I don't c any leading zeros   And   in document.   


AGe.   Format.  I3.    I3 = Integer 3 so what format new age should be numeric or character


Super User
Super User
Posts: 6,500

Re: Adding leading zeros to numeric fields

How are you making the CSV file?

You can read the CSV file with a data step and dump the lines to the SAS log to see if there are leading zeros.  For example this program will show the first 10 lines.

data _null_;

  infile 'myfile.csv' obs=10 ;

  input;

list;

run;

If the column is intended to be integer then why do you want leading zeros?

Also the column is specified as length 3 so why are you not using Z3 format?  Age should not be negative.  People do live to be over 99 years old.

Frequent Contributor
Posts: 120

Re: Adding leading zeros to numeric fields

I Didn't understand what u you have said

i have not used z3 because for EG

23

3

4

i want only

23

03

04

but if I use z3

the he output will be

023

003

004

but in I3 format one byte will be for sign and other two for digits 

Super User
Super User
Posts: 6,500

Re: Adding leading zeros to numeric fields

How did you generate a CSV file from the SAS dataset?  Did you call PROC EXPORT?  Did you use ODS CSV ?  Did you use a data step with DSD option on the FILE statement?  Did you use some functionality of Enterprise Guide?

Why do you think that you need a leading zero?  What software are you planning to use the resulting CSV file with that would require the leading zero?

Perhaps you just need to explicitly include the sign into the new character variable?

char_age = cats('+',put(age,z2.)) ;

What system is defining I3 to mean sign plus two digits?  I3 looks like a FORTRAN format specification and the sign is not required, but takes up one of the characters if present.  So '001' would be valid and so would '+01' or '-01'.  But '-101' would be too long since it would require 4 characters instead of three.

Are you sure that you even want a CSV file?  If your data definition is explicitly listing the number of columns for each variable then perhaps you want to generate a fixed field file instead of a comma separated file?

Frequent Contributor
Posts: 120

Re: Adding leading zeros to numeric fields

HHi tom ,

to age variable I used the syntax u gave me the result is

+36

+02

+04

i am using SAS EG I  am exporting SAS dataset using export to in SaS EG and saving in my c drive as a Csv file

Ask a Question
Discussion stats
  • 16 replies
  • 2132 views
  • 1 like
  • 6 in conversation