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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 1169 views
  • 3 likes
  • 3 in conversation