DATA Step, Macro, Functions and more

Replace missing variables (na) by random numbers within range

Accepted Solution Solved
Reply
Super Contributor
Posts: 383
Accepted Solution

Replace missing variables (na) by random numbers within range

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.


Accepted Solutions
Solution
‎05-15-2018 02:41 PM
SAS Super FREQ
Posts: 4,272

Re: Replace missing variables (na) by random numbers within range

[ Edited ]

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.

View solution in original post


All Replies
Solution
‎05-15-2018 02:41 PM
SAS Super FREQ
Posts: 4,272

Re: Replace missing variables (na) by random numbers within range

[ Edited ]

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.

Super Contributor
Posts: 383

Re: Replace missing variables (na) by random numbers within range

Hi Risk, yes, it can be numeric. Thanks!
Super Contributor
Posts: 383

Re: Replace missing variables (na) by random numbers within range

[ Edited ]

@Rick_SAS

 

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.

 

 

SAS Super FREQ
Posts: 4,272

Re: Replace missing variables (na) by random numbers within range

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).

Super Contributor
Posts: 383

Re: Replace missing variables (na) by random numbers within range

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 140 views
  • 1 like
  • 2 in conversation