BookmarkSubscribeRSS Feed
Hart
Calcite | Level 5
I am importing a txt file to sas. the ID field has leading zeros which i wish to retain. after importing i find that the zeros are not being imported. how can I imported the data while retaining the zeros.

thanks
2 REPLIES 2
Cynthia_sas
Diamond | Level 26
Hi:
Partly the answer depends on what you want to do with ID and whether ID is character or numeric. If ID is numeric, then SAS will not -store- leading zeroes -- however, you can -display- leading zeroes by simply using a FORMAT statement (probably best in this case to permanently associate a format with the ID variable).
[pre]format ID z5.;
[/pre]

If you put that format statement in the program where you read the text file (assuming you're using a DATA step program), then SAS would format the ID to be 5 digits, with leading zeroes. So the internally stored number '123' would display on all reports as '00123', for example. If you are importing the data without a DATA step, then you can always modify the format attribute with PROC DATASETS, after the dataset is created.

However, if you read ID as a character variable and then store it as a character variable, SAS will both -store- and -display- the leading zeroes without needing a user defined format.

cynthia
[pre]
data leadzero;
infile datalines;
input name $ as_num_id as_char_id $;
format as_num_id z5.;
return;
datalines;
alan 00123 00123
bob 00234 00234
cathy 00345 00345
dana 00456 00456
;
run;

proc print data=leadzero;
run;
[/pre]
Hart
Calcite | Level 5
thanks format z7. did the trick

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

Register now

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
  • 2 replies
  • 1243 views
  • 0 likes
  • 2 in conversation