BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kmardinian
Quartz | Level 8

Hi, I would like to create a list of 25 randomly generated numbers that start with the two letters "AB----" ex: AB14587, AB65899, AB36525, etc.

 

I looked into the RAND function, but can't seem to figure out how I would incorporate the "AB-" in there.

 

Any help would be much appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Modifying @PeterClemmensen solution very slightly to use Z5 format so that you always have a string of the same length.

 

data want(drop=i);
   length num $20;
   do i=1 to 25;
      num=cats("AB", put(rand('integer', 1, 10000), z5.));
      output;
   end;
run;

View solution in original post

7 REPLIES 7
noling
SAS Employee

You could combine the output from this link with what you already have for the numeric portion: http://support.sas.com/kb/51/386.html

 

This would be combining 2 random elements: the text and the numbers.


Register today and join us virtually on June 16!
sasglobalforum.com | #SASGF

View now: on-demand content for SAS users

PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data want(drop=i);
   length num $20;
   do i=1 to 25;
      num=cats("AB", rand('integer', 100, 10000));
      output;
   end;
run;
Reeza
Super User

Modifying @PeterClemmensen solution very slightly to use Z5 format so that you always have a string of the same length.

 

data want(drop=i);
   length num $20;
   do i=1 to 25;
      num=cats("AB", put(rand('integer', 1, 10000), z5.));
      output;
   end;
run;
ballardw
Super User

@kmardinian wrote:

Hi, I would like to create a list of 25 randomly generated numbers that start with the two letters "AB----" ex: AB14587, AB65899, AB36525, etc.

 

I looked into the RAND function, but can't seem to figure out how I would incorporate the "AB-" in there.

 

Any help would be much appreciated!


Being a bit pedantic but unless AB are HEX digits then you are NOT generating random "numbers".

If you cast the question as "I would like to create a string value that starts with AB- followed by a random number" you might even have figured it out yourself.

 

Is there a specific range in the number of digits the number part must possess?

Reeza
Super User
What about duplicates? Are duplicates allowed?
kmardinian
Quartz | Level 8

Duplicates would not be allowed.

 

I tried it using the do loop and it worked the way I needed it to!

 

Thank you!

Reeza
Super User

@kmardinian wrote:

Duplicates would not be allowed.

 

I tried it using the do loop and it worked the way I needed it to!

 

Thank you!


My solution may generate duplicates.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1982 views
  • 2 likes
  • 5 in conversation