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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

3 REPLIES 3
Rwon
Obsidian | Level 7

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

Shmuel
Garnet | Level 18

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;
Astounding
PROC Star

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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