How to replace missing variables (na) by random numbers between 1 through 10? numbers can be repeated.
data x;
input a $;
cards;
1
2
na
na
4
na
na
;
Thanks millions in advance.
Shouldn't 'a' be a numeric variable?
Anyway, see the article "How to generate random integers in SAS".
If you are reading a numeric variable x, then the modern code is
if missing(x) then x = rand("Integer", 1, 10);
Here I am assuming that you want a random integer. If you are using an old version of SAS, see the article for an alternative syntax.
Shouldn't 'a' be a numeric variable?
Anyway, see the article "How to generate random integers in SAS".
If you are reading a numeric variable x, then the modern code is
if missing(x) then x = rand("Integer", 1, 10);
Here I am assuming that you want a random integer. If you are using an old version of SAS, see the article for an alternative syntax.
Do you see anything wrong here? Month_of_birth is now a numeric variable with missing .
data want; set have;
if missing(Month_of_birth) then Month_of_birth=rand("Integer", 1, 31);
run;
ERROR: The first argument of the RAND function must be a character string with a value of
BERNOULLI, BETA, BINOMIAL, CAUCHY, CHISQUARE, ERLANG, EXPONENTIAL, F, GAMMA, GAUSSIAN,
GEOMETRIC, HYPERGEOMETRIC, LOGNORMAL, NEGB, NORMAL, POISSON, T, TABLE, TRIANGULAR, UNIFORM,
or WEIBULL.
As I indicated in my answer (and say explicitly in the article that I asked you to read), the "Integer" distribution was introduced in the past few releases of SAS. The error says that you are not using a recent version of SAS. See the article for how to define the %RandBetween macro and use %RandBetween(1,31).
Updating what worked:
%macro RandBetween(min, max);
(&min + floor((1+&max-&min)*rand("uniform")))
%mend;
data success; set have;
if Month_of_birth=. then Month_of_birth = %RandBetween(1, 12);
run;
Thanks @Rick_SAS
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.