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
SAS Super FREQ
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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 689 views
  • 0 likes
  • 2 in conversation