BookmarkSubscribeRSS Feed
SASNewbie7
Calcite | Level 5

Hi everyone,

 

I'm in a data management class right now and have been given the following assignment: 

 

Create a temporary SAS dataset with 7000 observations containing a numeric subject number (beginning with 1), a randomly generated marital status (use CALL STREAMINIT and the RAND function with a uniform distribution) with the following proportions: married 65%, single 25%, divorced 10% and a randomly generated age (integer) ranging from 25 to 50 inclusive. Compute the frequencies for marital status and age to confirm values have been appropriately assigned. Print the first 20 observations. {BONUS 5 points: review and comment on the relationship between age and marital status. Within each age, is marital status approximately 65% married, 25% single, and 10% divorced?  Why or why not?}

 

I have thus far developed the following code:

data temp;
call streaminit(1234567);
do Subj = 1 to 7000;
marital = ceil(rand("uniform", 0, 3));
age = ceil(rand("uniform", 24, 50));
output;
end;
run;
proc freq data = temp;
tables marital age;
run;

 

How can I adjust the proportions of the marital variable so that the random numbers generate in the specified proportions described above? They are currently generating in a 33% proportion for each number 1-3.

 

Thank you.

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Use the rand('uniform') function. If the result is 0 to 0.65, this is "married", 0.65 to 0.9 as "single", and 0.9 to 1.0 is "divorced".

--
Paige Miller
Reeza
Super User

You can also look up the rand('table') option when you have variables that have different predetermined probability. 

 


@SASNewbie7 wrote:

Hi everyone,

 

I'm in a data management class right now and have been given the following assignment: 

 

Create a temporary SAS dataset with 7000 observations containing a numeric subject number (beginning with 1), a randomly generated marital status (use CALL STREAMINIT and the RAND function with a uniform distribution) with the following proportions: married 65%, single 25%, divorced 10% and a randomly generated age (integer) ranging from 25 to 50 inclusive. Compute the frequencies for marital status and age to confirm values have been appropriately assigned. Print the first 20 observations. {BONUS 5 points: review and comment on the relationship between age and marital status. Within each age, is marital status approximately 65% married, 25% single, and 10% divorced?  Why or why not?}

 

I have thus far developed the following code:

data temp;
call streaminit(1234567);
do Subj = 1 to 7000;
marital = ceil(rand("uniform", 0, 3));
age = ceil(rand("uniform", 24, 50));
output;
end;
run;
proc freq data = temp;
tables marital age;
run;

 

How can I adjust the proportions of the marital variable so that the random numbers generate in the specified proportions described above? They are currently generating in a 33% proportion for each number 1-3.

 

Thank you.

 


 

ballardw
Super User

Marital = rand('table', 0.65, 0.25, 0.10);

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1327 views
  • 0 likes
  • 4 in conversation