I want to create a variable block size in randomization in sas.Block size will be multiple of 8 (3:1:3:1). It will look like something like below (In the example we have 8 obs for block 1,8 obs for block 2,16 for block 3,24 for block 4)
var1 | block | var2 | var3 |
1 | 1 | L | 1 |
2 | 1 | L | 1 |
3 | 1 | L | 1 |
4 | 1 | L | 1 |
5 | 1 | L | 1 |
6 | 1 | L | 1 |
7 | 1 | L | 1 |
8 | 1 | L | 1 |
9 | 2 | L | 1 |
10 | 2 | L | 1 |
11 | 2 | L | 1 |
12 | 2 | L | 1 |
13 | 2 | H | 1 |
14 | 2 | L | 1 |
15 | 2 | L | 1 |
16 | 2 | L | 1 |
17 | 3 | L | 1 |
18 | 3 | H | 1 |
19 | 3 | L | 1 |
20 | 3 | L | 1 |
21 | 3 | L | 1 |
22 | 3 | L | 1 |
23 | 3 | H | 1 |
24 | 3 | L | 1 |
25 | 3 | L | 1 |
26 | 3 | L | 1 |
27 | 3 | L | 1 |
28 | 3 | L | 1 |
29 | 3 | L | 1 |
30 | 3 | L | 1 |
31 | 3 | L | 1 |
32 | 3 | L | 1 |
33 | 4 | L | 1 |
34 | 4 | L | 1 |
35 | 4 | L | 1 |
36 | 4 | L | 1 |
37 | 4 | L | 1 |
38 | 4 | L | 1 |
39 | 4 | L | 1 |
40 | 4 | L | 1 |
41 | 4 | L | 1 |
42 | 4 | L | 1 |
43 | 4 | L | 1 |
44 | 4 | L | 1 |
45 | 4 | L | 1 |
46 | 4 | L | 1 |
47 | 4 | L | 1 |
48 | 4 | L | 1 |
49 | 4 | L | 1 |
50 | 4 | L | 1 |
51 | 4 | L | 1 |
52 | 4 | L | 1 |
53 | 4 | L | 1 |
54 | 4 | L | 1 |
55 | 4 | L | 1 |
56 | 4 | L | 1 |
Do you want code that is dynamic and figures out the block size? Or do you want to just hard-code 8 8 16 24?
I need a dynamic code that will return the block size as multiple of 8 (random).
Do you have a dataset and you want just to generate the BLOCK variable or
do you want to generate the whole dataset?
You can use RANUNI function or any other randomization sas function in order to
define the block size, something like:
retain block_size;
block_size = int(ranuni(-1) * 80); /* any value in range 1-80 */
do i=1 to block_size;
/* any other computation, assiging value to variables */
OUTPUT;
end;
Insert that code in the data step - either to generate a new dataset or to add observation to a given one.
UNTESTED CODE
proc summary nway data=have;
class block;
var var1;
output out=block_size n=block_size;
run;
data want;
merge have block_size;
by block;
run;
There's nothing here that forces the block size to be a multiple of 8, it just computes the number of records in each block.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.