Help using Base SAS procedures

Assigning repeated values for a variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Assigning repeated values for a variable

Let's say I have 100 Temporary Employees, variable label TE, and they are assigned a number 1 -100. I want to assign the Temp Employ a Rotation number, variable label RO, of 1 through 3 repeatedly and have it start with a random rotation number. So if the random number generated for the first TE is RO = 2, I want the next RO=3, then start the cycle over of assigning RO of 1-3 for the rest of the TEs. I know this is basic, but I am struggling how to write this.

here is my example:

Data haveemp;

INPUT TE;

Datalines;

1

2

3

4

5

6

7

8

9

10

11

12

;

data wantrot;

length RO 8;

set haveemp;

if TE = 1 then RO = ceil(4*ranuni(0));

Do i = 2 to 12;

...

end;

run;

I am not sure what to do after the do statement. I have a mod(n-1,3)+1 type of thinking for the other ROs but I am not sure how to ensure a proper cycle with the random number generated for the first TE.

Any help would be appreciated.


Accepted Solutions
Solution
‎06-10-2015 01:38 PM
PROC Star
Posts: 1,231

Re: Assigning repeated values for a variable

Maybe something like this:

127  data want;
128    set haveemp;
129    retain RO;
130    if TE=1 then RO=ceil(3*ranuni(0));
131    else RO=mod(RO,3)+1;
132    put _all_;
133  run;

TE=1 RO=3 _ERROR_=0 _N_=1
TE=2 RO=1 _ERROR_=0 _N_=2
TE=3 RO=2 _ERROR_=0 _N_=3
TE=4 RO=3 _ERROR_=0 _N_=4
TE=5 RO=1 _ERROR_=0 _N_=5
TE=6 RO=2 _ERROR_=0 _N_=6
TE=7 RO=3 _ERROR_=0 _N_=7
TE=8 RO=1 _ERROR_=0 _N_=8
TE=9 RO=2 _ERROR_=0 _N_=9
TE=10 RO=3 _ERROR_=0 _N_=10
TE=11 RO=1 _ERROR_=0 _N_=11
TE=12 RO=2 _ERROR_=0 _N_=12

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: Assigning repeated values for a variable

I like PROC PLAN for this.

data _null_;
  
call streaminit(34567);
   call symputx('ib',rand('table',1/3,1/3));
   run;
%put NOTE: &=ib &=sysrandom;

proc plan seed=998877;
  
factors to=100 ordered ro=1 of 3 cyclic(&ib) /noprint;
  
output out=plan;
   run;
  
quit;
proc print;
  
run;

6-10-2015 12-32-46 PM.png
Solution
‎06-10-2015 01:38 PM
PROC Star
Posts: 1,231

Re: Assigning repeated values for a variable

Maybe something like this:

127  data want;
128    set haveemp;
129    retain RO;
130    if TE=1 then RO=ceil(3*ranuni(0));
131    else RO=mod(RO,3)+1;
132    put _all_;
133  run;

TE=1 RO=3 _ERROR_=0 _N_=1
TE=2 RO=1 _ERROR_=0 _N_=2
TE=3 RO=2 _ERROR_=0 _N_=3
TE=4 RO=3 _ERROR_=0 _N_=4
TE=5 RO=1 _ERROR_=0 _N_=5
TE=6 RO=2 _ERROR_=0 _N_=6
TE=7 RO=3 _ERROR_=0 _N_=7
TE=8 RO=1 _ERROR_=0 _N_=8
TE=9 RO=2 _ERROR_=0 _N_=9
TE=10 RO=3 _ERROR_=0 _N_=10
TE=11 RO=1 _ERROR_=0 _N_=11
TE=12 RO=2 _ERROR_=0 _N_=12

Occasional Contributor
Posts: 14

Re: Assigning repeated values for a variable

Thanks, yes that worked.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 245 views
  • 3 likes
  • 3 in conversation