What statements do I add after the DO statement to randomly impute gender values of 'F' and 'M' (with 60% of people being female).
data imputed;
set missing;
if geneder = ' ' then do;
..........????
..........????
end;
run;
The below sample code should guide you through.
data have ;
input id sex $ ;
datalines ;
23 M
25 M
26 F
27 F
28 F
;
RUN ;
DATA WANT;
length Y $10. ;
set have ;
if sex='M' then do Y='Male' ;
end ;
else if sex = 'F' then do Y='Female' ;
end ;
run ;
proc print data=want;
run;
anoopmohandas7, that didn't help. 😞
I need to replace the missing observations by generating Ms and Fs randomly in a 4 to 6 ratio....
Please see PGStats reply for what I was looking for.
Thank you so much for your reply though! 🙂
Use the random number generator RAND which returns a random number uniformly distributed between 0 and 1 :
data imputed;
set missing;
if missing(gender) then
if rand("uniform") < 0.6 then gender = "F"; else gender ="M";
run;
Thank you, PG Stats! This does the trick! 😉
@PGStats: You're the statistician .. I'm only the Psychologist, but wouldn't LE .5 be the more appropriate cutoff?
Art, CEO, AnalystFinder.com
My question stated 0.6-PGStats just gave me what I asked for 😉
I hadn't seen your 4 to 6 ratio post. @PGStats: my sincere appologies. I should have know better than to question you 🙂
Art, CEO, AnalystFinder.com
@art297, you are welcome to question me any time
@PGStats idea of using RAND is what I'd use, but not a Uniform Distribution, but a Bernoulli.
if rand('bernoulli', 0.6) = 1 then sex='F';
else sex='M';
Same thing, under the hood. You could also use "table".
@PGStats wrote:
Same thing, under the hood. You could also use "table".
I was wondering about that...it's been a while since I've studied random number generators 🙂
Of course, I haven't seen what's under the hood, I can only guess. But as far as I know, all RAND generators are based on transformations of the uniform generator.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.