Write and run SAS programs in your web browser

Imput Missing Value

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Imput Missing Value

Hi all,

 

I am using SAS University Edition v.9.4. I am imputing missing value in my panel data and I need your help.


Below a sample data step:

data have;
input cust_id trans_max trans_min trans_avg;
cards;
1 10 2 6
2 5 2 .
3 20 2 11
4 14 4 .
5 3 5 .

 As you can see, the variable "trans_avg" is nothing else that the arithmetic average between the variables "trans_max" and "trans_min".

 

In other words "trans_avg" is equal to:


trans_avg=(trans_max+trans_min)/2

 

How can I tell SAS to replace the missing values with the arithmetic average between trans_max and trans_min?

I wanted to use a code as simple as the one shown below, but SAS cannot read my formula:

 

 

data new;
set have;
if trans_avg=. then trans_avg=((trans_max+trans_min)/2);
run;

I know this code is wrong, but I am not so good at using SAS yet. So, hopefully you can help.

Thank you in advance.


Accepted Solutions
Solution
‎07-18-2017 04:18 PM
Trusted Advisor
Posts: 1,394

Re: Imput Missing Value

As long trans_max and trans_min are bot not missing values,

you can use your own code:

if trans_avg=. then trans_avg=(trans_max+trans_min) / 2;

only, if there is a possiblity that one of the two are missing valuae, then  using + in oreder to summarize will result in missing value,

then @Rwon is right and you better change it to:

trans_avg = sum(trans_max, trans_min) / 2;

View solution in original post


All Replies
Contributor
Posts: 22

Re: Imput Missing Value

[ Edited ]

When you add missing numbers with the plus (+) sign, SAS will return a missing value. 

 

If you still wish to calculate the sum and treat the missing values as zeroes, use the sum function. You wouldn't need to add the if condition, since it will calculate the average for each row.

data new;
set have;
trans_avg=(sum(trans_max, trans_min)) / 2;
run;

 

Reference:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245953.htm

http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p17j9efnrdlz0gn1d19e...

Solution
‎07-18-2017 04:18 PM
Trusted Advisor
Posts: 1,394

Re: Imput Missing Value

As long trans_max and trans_min are bot not missing values,

you can use your own code:

if trans_avg=. then trans_avg=(trans_max+trans_min) / 2;

only, if there is a possiblity that one of the two are missing valuae, then  using + in oreder to summarize will result in missing value,

then @Rwon is right and you better change it to:

trans_avg = sum(trans_max, trans_min) / 2;
Super User
Posts: 5,085

Re: Imput Missing Value

Your program looks fine.  If you had trouble with it, just post the log so we can see what happened.

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 154 views
  • 3 likes
  • 4 in conversation