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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

5 REPLIES 5
Rick_SAS
SAS Super FREQ

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.

Cruise
Ammonite | Level 13
Hi Risk, yes, it can be numeric. Thanks!
Cruise
Ammonite | Level 13

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

 

 

Rick_SAS
SAS Super FREQ

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

Cruise
Ammonite | Level 13

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 5 replies
  • 1819 views
  • 1 like
  • 2 in conversation