Need help on generating random consistent first names

Reply
Occasional Contributor
Posts: 14

Need help on generating random consistent first names

I have a task that I need to accomplish. I have FirstName that I need to either switch around or assign random names to it.

FirstName

John

Joseph

Charles

Charles

John

John

Mary

John

FirstName (randomized) (Note: These are just random names)

Bill

Water

Salt

Salt

Bill

Bill

Mark

Bill

The goal is to assign consistent random names or switch the characters around using a logic. This logic must however generate consistent translation. For instance, in the above dataset, all occurrences of John and Charles must be translated to Bill (or any name generated by your code) and Salt (or any name generated by your code) respectively as with the random test data

Thanks and I look forward to hearing from you all.

Super User
Posts: 11,121

Re: Need help on generating random consistent first names

Does your "randomization" require that no name have a chance of being assigned it's original value?

If not this is one approach that scrambles the names without introducing any new words. The smaller the number of unique names the larger the chance that one or more of the names will be assigned it's original value.

data start;
   input Name $;
datalines;
John
Joseph
Charles
Charles
John
John
Mary
John
Fred
Lincoln
George
June
April
Leticia
Summer
Marge
;
run;

proc sql;
   create table uniquenames as
   select distinct Name
   from start;
quit;

data temp;
   set uniquenames;
   order=_n_;
   rand = ranuni(123);
run;

proc sort data=temp out=temp2; by rand;run;

data cntldata;
   merge
      temp
      temp2 (rename=(name=NewName) drop=order)
   ;
   fmtname = "Rename";
   start=Name;
   label = NewName;
   type='C';
run;
proc format library=work cntlin=cntldata;run;

proc print data=start;
var name;
format name $rename.;
run;

Note: you could use the format $rename to either actually change the value in a data step:

name = put(name,$rename.);

or create a new variable

RandName = put(name,$rename.);

or neither and just associate the format when using the variable.

Occasional Contributor
Posts: 14

Re: Need help on generating random consistent first names

The names can be assigned it's original value so long the percentage of such names are insignificant. Also, the names don't have to be meaningful names. Thanks

Ask a Question
Discussion stats
  • 2 replies
  • 170 views
  • 0 likes
  • 2 in conversation