How to create a unique identifier in a defined format

Reply
New Contributor
Posts: 3

How to create a unique identifier in a defined format

I need to create a unique identifier with a set number of characters - leading with zeroes.  So I use the _n_ variable to get the number for each record.  But I need to convert the _n_ variable to an 8-digit character leading with zeroes.  So _n_=1, I need a value of 00000001.  If _n_ if 2345, then the value is 00002345.

Thanks, Seth

Super User
Posts: 17,864

Re: How to create a unique identifier in a defined format

Use PUT to convert to character with the Z8. format.

put(_N_, Z8.)

New Contributor
Posts: 3

Re: How to create a unique identifier in a defined format

Sorry but I'm a novice at this.  I did the following:

Data x;

    Set x;

    Identifier = put(_N_, Z8.);

Run;

I'm still getting 1, 2, 3, 4, etc.  Not 00000001, 00000002, etc.

Super User
Posts: 17,864

Re: How to create a unique identifier in a defined format

Try the following instead:

Data X2;

set x;

identifier=put(_N_, z8.);

run;

If you've run this a bunch of times you may need to re-create your original X data set.

I find that for debugging its easier to create new datasets rather than re-write old ones.

Here's some code that shows you it does work for sure:

data want;

    set sashelp.class;

        identifier=put(_n_, z8.);

run;

New Contributor
Posts: 3

Re: How to create a unique identifier in a defined format

Thanks - that works.

Ask a Question
Discussion stats
  • 4 replies
  • 194 views
  • 0 likes
  • 2 in conversation