BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_newbie3
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

2 REPLIES 2
PGStats
Opal | Level 21

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
ballardw
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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