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

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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