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 !
Use the RAND('table', ...) function
Example:
if x=1 then y=rand('table',0.01,0.1,0.4,0.49);
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.
Use the RAND('table', ...) function
Example:
if x=1 then y=rand('table',0.01,0.1,0.4,0.49);
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.