Home Forums Ask Anything Random Here create sequential number

  • create sequential number

  • christina

    Member
    November 25, 2020 at 11:41 pm
    Up
    0
    Down

    need to create id like this in sas

    start with ‘aa59’ followed by 6 digits, increasing by 1, starting at 000001.

    should be ‘aa59000001’

    and ‘aa59000002’

    thanks!

  • Datura

    Member
    November 26, 2020 at 7:04 am
    Up
    0
    Down

    Easy. Create a ID say numeric X by a do loop first, 1,2,3….. then convert it to char and pad with leading zeros using Z6. format. Finally, combine them together. You get it?

    Data AAA;
    Do X=1 to N;
    ID= “aa59” II put(X, Z6.);
    Output;
    End;
    Run;

    • This reply was modified 3 years, 12 months ago by  Datura.
    • This reply was modified 3 years, 12 months ago by  Datura.
    • This reply was modified 3 years, 12 months ago by  Datura.
    • This reply was modified 3 years, 12 months ago by  Datura.
    • This reply was modified 3 years, 11 months ago by  Datura.
  • christina

    Member
    November 26, 2020 at 7:49 am
    Up
    0
    Down

    Great thanks!

  • christina

    Member
    November 26, 2020 at 9:00 am
    Up
    0
    Down

    if i don’t know how many obs,how to define n in loop? if the data has 3000 obs or how to let it stop the loop when there is no more rows?

  • Datura

    Member
    November 26, 2020 at 9:15 am
    Up
    0
    Down

    Use nobs option, or _N_, or a macro variable. It depends on what you need.

    • This reply was modified 3 years, 12 months ago by  Datura.
    • This reply was modified 3 years, 12 months ago by  Datura.
  • christina

    Member
    November 26, 2020 at 10:28 am
    Up
    0
    Down

    Got it thanks!

Log in to reply.

Original Post
0 of 0 posts June 2018
Now