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.
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.
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
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.