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
Do something like this
data _null_;
array _{6} $ ('a','c','m','s','f','o');
idx=rand('integer', 1, dim(_));
RandLetter=_[idx];
put RandLetter=;
run;
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 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
RandLetter=char('acmsfo',rand('integer',6));
RandLetter=choosec(rand('integer',6),'a','c','m','s','f','o');
RandLetter=scan('a c m s f o',rand('integer',6));
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!
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.
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. 🙂
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.