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.
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;
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
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;
Your program looks fine. If you had trouble with it, just post the log so we can see what happened.
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.