Hello all,
am tring to create some random id in sas für about 1600 datasets. I have used most examples I search for but its not working.
I want something like a four integer id e.g. 1045
Can any one help?
I assume that these IDs has to be unique? Should the ID's simply be in a single numeric variable or?
Yes, they should
Here is one way
data test;
do id=1 to 9999;
output;
end;
run;
proc surveyselect data=test
method=srs n=1600 out=want;
run;
proc datasets lib=work nolist;
modify want;
format id z4.;
run;quit;
Try function UUIDGEN()
ok thanks,
let me try an see
Here's the long winded approach I usually use.
/*This program demonstrates how to create a basic anonymized
key for a unique identifier. Ensure you set the value in CALL
STREAMINIT()/RANDOM_SEED macro variable to ensure you can
replicate the keys if needed*/
%let random_seed = 30;
*list of unique values;
proc sql;
create table unique_list as
select distinct name
from sashelp.class;
quit;
*add random values;
data random_values;
set unique_list;
call streaminit(&random_seed.);
rand = rand('normal', 50, 10);
run;
*sort;
proc sort data=random_values;
by rand;
run;
*Assign ID to N, note this is a character format;
data ID_key_pair;
set random_values;
label = put(_n_, z5.);
fmtname = 'anon_fmt';
type='C';
start=name;
run;
*Create a format;
proc format cntlin=id_key_pair;
run;
*Create dataset with anonymized IDs;
data want;
set sashelp.class;
RandomID = put(name, $anon_fmt.);
*drop name;
run;
@Anita_n wrote:
Hello all,
am tring to create some random id in sas für about 1600 datasets. I have used most examples I search for but its not working.
I want something like a four integer id e.g. 1045
Can any one help?
Hi @Anita_n
You need a 4-digit ID, but I wonder why you want it created as a random number in addition to being unique. How do you plan to use the ID, since a simple sequence number will not work?
But if this is essential, you could create a random number, sort the data set on the random number, so the observarions are shuffled like a card deck, and then replace the random number with a sequence number as a 4-digit ID.
* Test data set;
data test;
do orig_sequence = 1 to 9999;
output;
end;
run;
%macro AddRandomID(ds, dsout);
* Add a random bumber;
data w1; set &ds;
random_number = ranuni(5);
run;
* Sort by random number;
proc sort data=w1; by random_number;
run;
* Replace random number with sequence number as ID variable;
data &dsout; set w1(drop=random_number);
format ID z4.;
ID = _N_;
run;
* Sort back to original sequence to demonstrate the "randomness";
proc sort data=&dsout; by orig_sequence;
run;
%mend;
%AddRandomID(test,test_id);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.