DATA Step, Macro, Functions and more

Get the combinations with do loop

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Get the combinations with do loop

What I want is to create a dataset which has three columns. The first column is digit(1-5). The second column is (A-C). The third column is a random number(0,1)

The sample:

1    A    0.1    
1    B    0.2    
1    C    0.5    
2    A    0.8    
2    B    0.3    
2    C    0.5    
3    A    0.2    
3    B    0.2    
3    C    0.3    
...
5    A    0.3    
5    B    0.9    
5    C    0.3    

My code:

data DESIGN(drop=i j k);
array result[3] $ Block Treatment RandomDigit;
do i = 1 to 5;
    do j = 1 to 3;
        do k = 1 to 3;
            if(k=1) then result[i] = i;
            if(k=2) then result[i]=j;
            if(k=3) then result[i]=RAND("Uniform");
         
        end;
    end;
end;
datalines;
run;

Thanks for correct my errors.


Accepted Solutions
Solution
‎10-29-2016 08:12 PM
Respected Advisor
Posts: 4,649

Re: Get the combinations with do loop

You have many SAS programming concepts mixed up in your code. Understanding how the following code works should help you:

 

data design;
call streamInit(7768756); /* seed for random numbers */
do block = 1 to 5;
	do treatment = "A", "B", "C";
		randomDigit = round(rand("UNIFORM"), 0.1);
		output;
		end;
	end;
run;

proc print; run;
PG

View solution in original post


All Replies
Solution
‎10-29-2016 08:12 PM
Respected Advisor
Posts: 4,649

Re: Get the combinations with do loop

You have many SAS programming concepts mixed up in your code. Understanding how the following code works should help you:

 

data design;
call streamInit(7768756); /* seed for random numbers */
do block = 1 to 5;
	do treatment = "A", "B", "C";
		randomDigit = round(rand("UNIFORM"), 0.1);
		output;
		end;
	end;
run;

proc print; run;
PG
Super User
Posts: 10,500

Re: Get the combinations with do loop

The ability to use lists of values in Do loops and to use Character values as loop indices is one of the nice things about SAS. There were a few times when I was coding in FORTRAN I would cheerfully have taken a compiler designer out for drinks if they would include such things.

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 265 views
  • 3 likes
  • 3 in conversation