Renee
Renee

Reputation: 1

How to select variable (with a year in the name) for calculation based on value of a date field using SAS SQL

With SAS SQL (or just SAS) I need to use a variable for a calculation based on the year portion of a different date field. The variable's name contains the year that I'd need to match from the year portion of the other date variable. How can I select the right variable to use for my calculation?

For example, I need to select which one of these to use:

GRADE_2013
GRADE_2014
GRADE_2015 

by looking at a date field of the format 15JAN2014 - so from that year of 2014 I want to grab the value from GRADE_2014 to use in another calculation.

Upvotes: 0

Views: 91

Answers (3)

user3791254
user3791254

Reputation: 45

Thanks so much for the great answers...I'm a novice so I'll have to go the non-array approach at least to start.

Upvotes: 0

Quentin
Quentin

Reputation: 6378

Generally, this sort of problem becomes much easier if you can transpose your data into a more normalized format.

So instead of having three grade_YYYY variables with year suffixes on each, transpose each record into three records, with variables YEAR and GRADE.

Upvotes: 0

Reeza
Reeza

Reputation: 21274

You have a few options, one is an array with a year index and another is the VVALUEX function that looks up the value of a variable.

Data One;
set Have;
array grades(2013:2015) grade_2013-grade_2015;

*Array method;
variable_want1 = grades(year(date_field));

*VValueX method;
variable_want2 = vvalues('grades_'||put(year(date_field), 4.));
run;

Upvotes: 2

Related Questions