DATA Step, Macro, Functions and more

Creat missing value randomly

Reply
N/A
Posts: 0

Creat missing value randomly

Hi, I'm trying to creat new datasets that contains missing vlaues using exist data.

If I have a data that contains 7 continuous variables like below.

A B C D E F G
1 3 4 3 2 3 4
2 4 3 4 5 6 2
2 5 3 4 5 3 1
1 2 3 3 2 1 3
2 5 4 3 4 1 3

I want to make missing randomly, but same number of missing within each person like below.

data-1 (1 missing per person)

A B C D E F G
1 . 4 3 2 3 4
2 4 3 . 5 6 2
2 5 . 4 5 3 1
. 2 3 3 2 1 3
2 5 4 3 4 . 3

data-2 (2 missing per person)

A B C D E F G
1 . 4 3 . 3 4
2 4 . . 5 6 2
2 5 . 4 5 . 1
. 2 3 3 . 1 3
2 5 . 3 4 . 3

...up to 6 missings per person.

Also, is there any way to replace the missing value with mean of rest of values?

Thanks.
Valued Guide
Posts: 634

Re: Creat missing value randomly

Posted in reply to deleted_user
This DATA step will create the randomly missing values. A simple extension could be used to create n randomly missing values.
[pre]data missran;
input A B C D E F G;
array allvar {7} a--g;
* randomly set one var to missing;
loc = ceil(ranuni(0)*dim(allvar));
allvar{loc} = .;
* replace missing with mean of other vars;
allvar{loc} = mean(of a--g);
datalines;
1 3 4 3 2 3 4
2 4 3 4 5 6 2
2 5 3 4 5 3 1
1 2 3 3 2 1 3
2 5 4 3 4 1 3
run;
proc print data=missran;
run;
[/pre]
N/A
Posts: 0

Re: Creat missing value randomly

Thanks a lot!!
Ask a Question
Discussion stats
  • 2 replies
  • 119 views
  • 0 likes
  • 2 in conversation