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

i want to assign a random string to a variable. If say i have to pick a character from the below list. can you suggest me a code?

 

"a","c","m","s","f","o"

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data _null_;
   array _{6} $ ('a','c','m','s','f','o');
   idx=rand('integer', 1, dim(_));
   RandLetter=_[idx];

   put RandLetter=;
run;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data _null_;
   array _{6} $ ('a','c','m','s','f','o');
   idx=rand('integer', 1, dim(_));
   RandLetter=_[idx];

   put RandLetter=;
run;
VinitvictorCorr
Quartz | Level 8
i tried rand before, but it said it wouldn't work on character values,but this is great
FreelanceReinh
Jade | Level 19

@VinitvictorCorr wrote:
i tried rand before, but it said it wouldn't work on character values,but this is great

Alternative techniques to let RAND "work on character values" include

  • the CHAR function (for single characters):
    RandLetter=char('acmsfo',rand('integer',6));
  • the CHOOSEC function (for strings of any lengths):
    RandLetter=choosec(rand('integer',6),'a','c','m','s','f','o');
  • the SCAN function (for strings of any lengths):
    RandLetter=scan('a c m s f o',rand('integer',6));
novinosrin
Tourmaline | Level 20

Sir @FreelanceReinh   Really love your CHOOSEC. My oh my honestly didn't expect, While it's easy to acknowledge the charm of how your thought process works, I recall Guru @data_null__  effecting the use of such functions and many more to super effect in his SAS-L posts. I wonder if SAS-L still exists, and if yes DN is even on it anymore. Anyways,  Just goes to prove, great minds think alike i.e you, king, PG, hashman etc are in a league of your own. Class act!

FreelanceReinh
Jade | Level 19

Thanks, @novinosrin, but I'm not sure what's so inventive about using those character functions with a random number argument. 🙂

 

Regarding SAS-L: Since they started requiring a login even for read access (less than 6 years ago; at least my old links suddenly didn't work anymore, but led to a login screen) I haven't been there. But it was a valuable source of information for me in my earlier years (only reading, not posting). Luckily, a few of the SAS-L giants have joined the SAS Support Communities.

novinosrin
Tourmaline | Level 20

I meant, it's not about the inventive, it's just that not many would even think or search along the lines finding a function for a cause and would rather go with we/they know. I guess it's the presence of mind to look for what and where let alone how simple or complex that may be.

I am also glad that some of the SAS-L are right here. 🙂

 

VinitvictorCorr
Quartz | Level 8
wow,, thats awesome.. didnt know about these functions in sas.. Thanks 🙂
ballardw
Super User

And if for some reason you would like to have a random but not uniform chance of selection something like:

 

idx=rand('table', 0.2, 0.4, 0.1, 0.05, 0.15, 0.1);
RandLetter=_[idx];

You need to know the number of elements selected from and the numbers represent the percentage of selecting that position. They must add to 1 or you will either get a n+1 possible response if the sum is less than 1 with the probability not included in the total or examine the documentation as the possibilities get a bit harder to explain quickly.

sas-innovate-white.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
  • 9 replies
  • 5669 views
  • 5 likes
  • 5 in conversation