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

Hello, 
I would like to allocate a (categorical) variable ( Y=1,2,3,4) to each observation with a precise repartition, in function of another variable (X). I explain myself :


If X=0, I want :

 - 1% of the population with Y=1

 - 10% of the population with Y=2

 - 40%   of the population with Y=3

 - 49% of the population with Y=4

And if X=1, I want : 

 - 2% of the population with Y=1

 - 12% of the population with Y=2

 - 56%   of the population with Y=3

 - 30% of the population with Y=4

Etc until X=10

I know how to make the do loop and the conditional statement :
Do X=0 to 10 ;
If X=i then ...
i=i+1;

 

But I don't know how to allocate each observation to Y in the right proportions.
Of course, I know for each X what the repartition (in percentage) should be. I just need to know how to allocate the observations to each level of Y. 

Thank you by advance for your help !

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Use the RAND('table', ...) function

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

 

Example:

if x=1 then y=rand('table',0.01,0.1,0.4,0.49);
--
Paige Miller

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

How many different values can X have? 

 

Also, can you provide some sample data and what your desired result looks like? Makes it much easier to provide usable code.

PaigeMiller
Diamond | Level 26

Use the RAND('table', ...) function

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

 

Example:

if x=1 then y=rand('table',0.01,0.1,0.4,0.49);
--
Paige Miller
ballardw
Super User

Exactly how many observations are you going to process?

 

If you only have 25 records where X=1 it is going to be very hard to get any distribution that "exactly matches" the stated percentages. (1 value of y=1 is 4% in this case). Other similar issues will exist with other numbers of records with any given X value.

 

So what would expect for a "distribution" if you only have 25 X=1 records? or 50 X=0 (1% = half a record or Y=1 is 2%).

 

It is unlikely to be able to match any "exact distribution" if the number of X= records is not exactly 100 or a multiple of 100. Also and "exact distribution" likely is not going to be a true random distribution if that is what is desired.

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
  • 3 replies
  • 592 views
  • 0 likes
  • 4 in conversation