turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Replace missing variables (na) by random numbers w...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

a week ago

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

a week ago

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Cruise

a week ago - last edited a week ago

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.

All Replies

Solution

a week ago

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Cruise

a week ago - last edited a week ago

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

a week ago

Hi Risk, yes, it can be numeric. Thanks!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

a week ago - last edited a week ago

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Cruise

a week ago

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

a week ago

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