Help using Base SAS procedures

leading blanks

Reply
Contributor
Posts: 22

leading blanks

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
SAS Super FREQ
Posts: 8,868

Re: leading blanks

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]
Contributor
Posts: 22

Re: leading blanks

Posted in reply to Cynthia_sas
thanks format z7. did the trick
Ask a Question
Discussion stats
  • 2 replies
  • 130 views
  • 0 likes
  • 2 in conversation