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

Dear all,

I am using proc means to average values across multiple observations for each year. (see attached data). one complication is variable Horizon, some records have a special code -99, indicating this value might be missing or subject to error. In such cases, I want to average only those valid values,

e.g. For year_FPE=1996, avgHORI=(5/304+4.951+2.386)/3 (excluding -99)

How should I modify the code below ?

PROC MEANS DATA=id1300 NWAY noprint;

  CLASS Security_ID   YEAR_FPE;

  VAR horizon Specificity count;

  OUTPUT OUT=want  MEAN=avgHORI avgSPEC avgcount;

RUN;

thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

While Reeza's solution works, you would be well advised to change your data.  SAS supports special missing values, such as:

if horizon = -99 then horizon=.A;

There are 27 special missing values, .A through .Z, and ._ as well.

If you change the -99 to .A, the PROC MEANS will automatically throw them out without the need for a WHERE statement.  This becomes especially useful if you have two analysis variables.  For example, suppose COUNT could also be -99, but not necessarily on the same observations having HORIZON = -99.  Then there would be no WHERE statement that could remove the proper observations.  But if you have changed all -99 values to a special missing value, PROC MEANS would know enough to leave them out of the calculations.

Good luck.

View solution in original post

4 REPLIES 4
Reeza
Super User

Add in a where statement.

PROC MEANS DATA=id1300 NWAY noprint;

where horizon ne -99;

  CLASS Security_ID   YEAR_FPE;

  VAR horizon Specificity count;

  OUTPUT OUT=want  MEAN=avgHORI avgSPEC avgcount;

RUN;

Astounding
PROC Star

While Reeza's solution works, you would be well advised to change your data.  SAS supports special missing values, such as:

if horizon = -99 then horizon=.A;

There are 27 special missing values, .A through .Z, and ._ as well.

If you change the -99 to .A, the PROC MEANS will automatically throw them out without the need for a WHERE statement.  This becomes especially useful if you have two analysis variables.  For example, suppose COUNT could also be -99, but not necessarily on the same observations having HORIZON = -99.  Then there would be no WHERE statement that could remove the proper observations.  But if you have changed all -99 values to a special missing value, PROC MEANS would know enough to leave them out of the calculations.

Good luck.

Peter_C
Rhodochrosite | Level 12

If there is any concern about the inefficiency of re-writing all the data to take advantage of the "Astounding" idea, just create a VIEW to convert those -99 into special missings and implement that complex formula at the same time. For as PROC MEANS reads the view, the special values and formula will be calculated "on the fly".

Neat

Pete

LanMin
Fluorite | Level 6

Thanks to all of you!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 1017 views
  • 6 likes
  • 4 in conversation