DATA Step, Macro, Functions and more

Adding zeros for the number...

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 97
Accepted Solution

Adding zeros for the number...


hi,

I am providing a sample of dataset that I am working with...

I want threee zeros to be added to "id" in the ouput dataset;

DATA HAVE;

INFILE DATALINES;

INPUT ID NAME : $ AMOUNT;

DATALINES;

1 ALIERN 4500

2 VEREKR 20000

3 RERRRR 100000

;

RUN;

******************************

REQUIED OUTPUT;

00001 ALIERN 4500

00002 VEREKR 20000

00003 RERRRR 100000

Regards

ALLU


Accepted Solutions
Solution
‎02-02-2013 09:49 AM
Respected Advisor
Posts: 3,777

Re: Adding zeros for the number...

Not INFORMAT reading is not the issue.  You want to SEE it with leading zeros.

FORMAT AGE z5.;

You might want to consult the documentation!

View solution in original post


All Replies
Frequent Contributor
Frequent Contributor
Posts: 94

Re: Adding zeros for the number...

data get;

  format id2 $5.;

  set have;

  id2= '000'||strip(id);

  rename id2=id;

  drop id;

run;

Frequent Contributor
Posts: 97

Re: Adding zeros for the number...

thanks VD..

it is working...just i added another like

id2= '0000'||strip(id);

regards..

ALLU

Regular Contributor
Posts: 151

Re: Adding zeros for the number...

If it's for display purposes and you want to keep the id column as numeric, then just use the Zw.d format which pads the values with leading zeros.

e.g. format id z5.;

By the way, in your required output, you've added 4 zeros not 3.

Frequent Contributor
Posts: 97

Re: Adding zeros for the number...

Hi keith,

I am getting error....

will you please look in to the issue...

Regards

ALLU

448  data school;

449  input Age format z5. Quiz : $1. Midterm Final;

                      ---

                      48

ERROR 48-59: The informat Z was not found or could not be loaded.

450

451  id2= '000'||strip(age);

452  if  age=12 then grade=6;

453   else  if age=13 then grade=8;

454  datalines;

Solution
‎02-02-2013 09:49 AM
Respected Advisor
Posts: 3,777

Re: Adding zeros for the number...

Not INFORMAT reading is not the issue.  You want to SEE it with leading zeros.

FORMAT AGE z5.;

You might want to consult the documentation!

Frequent Contributor
Posts: 97

Re: Adding zeros for the number...

thanks...

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 349 views
  • 8 likes
  • 4 in conversation