BookmarkSubscribeRSS Feed
chennupriya
Quartz | Level 8

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

16 REPLIES 16
Astounding
PROC Star

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.

chennupriya
Quartz | Level 8

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 .

Tom
Super User Tom
Super User

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.

chennupriya
Quartz | Level 8

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

Patrick
Opal | Level 21

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.

chennupriya
Quartz | Level 8

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

CTorres
Quartz | Level 8

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

CTorres

ballardw
Super User

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

Patrick
Opal | Level 21

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.

chennupriya
Quartz | Level 8

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


Tom
Super User Tom
Super User

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.

chennupriya
Quartz | Level 8

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 

Tom
Super User Tom
Super User

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?

chennupriya
Quartz | Level 8

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 16 replies
  • 6528 views
  • 1 like
  • 6 in conversation