-2

How can I generate in SAS and ID code with 5 digits(letters & Numbers)? Where the first 3 must be letters and last 2 must be numbers.

zak
  • 1
  • 1
  • 1
    Show what you've already attempted - posting sample code & data will illicit more & better responses to your question. – Chris J Mar 24 '15 at 12:23
  • do you want to do that in a data step or in a macro? – DCR Mar 24 '15 at 14:13
  • how much ID's do you need ? – stallingOne Mar 24 '15 at 14:34
  • anything...macro or not .... 30 ID's – zak Mar 24 '15 at 16:10
  • Thank you so much! I started in this way, I defined all letters and numbers for an ID with length 5. I have above the code where the length is 5 with random letters/numbers but not with the required order. (like first 3 letters and last 2 digits). This is the part that I got stuck ....order data Generate; format Sample $8.; array x x1-x34 ( 1 up to 34); array y $ y1-y34 ; seed = 1234; k = 34; n= 10; sample = 'AAAAA'; do n=1 to n; call ranperk(seed, 5, of x1-x34); do i = 1 to k; y i = 'A'; if x i =1 then y i = '1'; if x i =2 then y i = '2'; *and so on; end; output; end; run; – zak Mar 24 '15 at 17:42

1 Answers1

0

You can create a unique mapping of the integers from 0 to 26^3 * 10^2 - 1 to a string of the format AAA00. This wikipedia page introduces the concept of different numerical bases quite well.

Your map would look something like this

value = 100 * (X * 26^2 + Y * 26^1 + Z * 26^0) + a * 10^1 + b * 10^0

where X, Y & Z are integers between 0 and 25 (which can be represented as the letters of the alphabet), and a & b are integers between 0 and 9.


As an example:

47416 = 100 * (0 * 26^2 + 18 * 26^1 + 6 * 26^0) + 1 * 10^1 + 6 * 10^0

Using:

0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z

You get:

47416 -> [0] [18] [6] (1) (6)
          A   S    G   1   6 

So 47416 can be represented as ASG16.


To do this programatically you will need to step through your number splitting it into quotient and remainder through division by your bases (10 and 26), storing the remainder as part of your output and using the quotient for the next iteration. you will probably want to use these functions:

  • mod() Modulo function to get the remainder from division
  • floor() Flooring function which returns the rounded down integer part of a real numer

A couple of similar (but slightly simpler) examples to get you started can be found here.

Have a go, and if you get stuck post a new question. You will probably get the best response from SO if you provide a detailed question, code showing your progress, a description of where and why you are stuck, any errors or warnings you are getting and some sample data.

SRSwift
  • 1,700
  • 9
  • 11