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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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