BookmarkSubscribeRSS Feed
mike_579357
Calcite | Level 5

Hello - I am relatively new to SAS and a first time poster. I have some code that assigns a random integer to a set of IDs. I have posted this below. Is it possible to create a new ID variable chosen by the user based on these random variables? Ive had trouble with this because the number of new IDs I want to assign inherently changes. Is there a better way to go about this?

 

data data_1;

  Input id_no;

  cards;

  123

  456

  789

  987

  654

;

run;

 

%macro assign_rand(no_rand=);

  %macro randbetween(min,max);

    (&Min + floor((1+&max-&min)*Rand("uniform)));

  %mend;

 

  data data_1;

    set data_1;

  random_no = %randbetween(1,&no_rand);

  run;

%mend;

 

4 REPLIES 4
Reeza
Super User

Are you looking for something along these lines?

https://gist.github.com/statgeek/fd94b0b6e78815430c1340e8c19f8644

 


@mike_579357 wrote:

Hello - I am relatively new to SAS and a first time poster. I have some code that assigns a random integer to a set of IDs. I have posted this below. Is it possible to create a new ID variable chosen by the user based on these random variables? Ive had trouble with this because the number of new IDs I want to assign inherently changes. Is there a better way to go about this?

 

data data_1;

  Input id_no;

  cards;

  123

  456

  789

  987

  654

;

run;

 

%macro assign_rand(no_rand=);

  %macro randbetween(min,max);

    (&Min + floor((1+&max-&min)*Rand("uniform)));

  %mend;

 

  data data_1;

    set data_1;

  random_no = %randbetween(1,&no_rand);

  run;

%mend;

 


 

Ksharp
Super User

data data_1;
  Input id_no;
  cards;
  123
  456
  789
  987
  654
;
run;


  data _null_;
    set data_1 end=last;
 if _n_=1 then call execute('data want;');
 call execute(catt('random_no=1 + floor(',id_no,'*Rand("uniform"));output;'));
 if last then call execute('run;');
  run;
ArtC
Rhodochrosite | Level 12

@Ksharp cool code, but would the following be a simpler approach?

  data want2;
   set data_1;
   random_no=1+floor(id_no*rand('uniform'));
   run;

Do we need to be sure that the random identifiers are unique?

Ksharp
Super User

@ArtC Cool !  Maybe I get confused by OP. Hope one day we could have a meet in the future.

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 759 views
  • 0 likes
  • 4 in conversation