3

I have created a numeric variable using the Prompt Manager in EG.

This variable is called HYr for the highest year of data that I am pulling.

When running the program I create 4 new variables based on the highest year and this is where I am having issues.

I have the following:

%Let Yr2 = &HYr. - 1;

%Let Yr3 = "&HYr." - 2;

%Let Yr4 = &HYr. - 3;

%Let Yr5 = '&HYr.' - 4;

I am trying to subtract the value from the year and the new variable will be used in determining date ranges that are being pulled. I am trying several things and learning in the process but I am still stuck.

I know it is probably just a simple syntax issue and given enough time I will probably be able to get it but no one in my office has any better SAS skills than I do and that isn't much.

thanks in advance.

Joe
  • 62,789
  • 6
  • 49
  • 67
tbirkett13
  • 31
  • 1
  • Why the downvote, I wonder? This is a pretty basic question but nonetheless a reasonable one in my opinion. Let's be welcoming of our new users who aren't strong programmers yet - they're the ones who need the most help! – Joe Apr 27 '18 at 15:41
  • Possible duplicate of [How to use arithmetic operators with SAS macro variables](https://stackoverflow.com/questions/39592223/how-to-use-arithmetic-operators-with-sas-macro-variables) – Reeza Apr 27 '18 at 15:58
  • 1
    I'm almost inclined to close it the other way (Close that one in favor of this one); this one seems much "cleaner" of an answer of how to use %eval since the other one has so much extra stuff. What do you think @reeza? – Joe Apr 27 '18 at 18:55
  • @joe I didn't know that was possible :D but yes, it makes sense to me to close the other one. Not sure how SO rates the quality of questions/answers in that regard. – Reeza Apr 27 '18 at 19:17
  • 1
    @Reeza It's appropriate when one is a much better question and/or has better answers that are more generally appropriate. And I can do it with the wave of my hand (as can Tom I believe now). But you could do it also, just takes 5 votes until you get that gold badge.. – Joe Apr 27 '18 at 21:05
  • consider using `%sysevalf` or `%eval` – samkart Apr 29 '18 at 06:37

1 Answers1

4

Use %EVAL() to do calculations with integers and macro variables.

%let HYR = 2018;
%Let Yr2 = %eval(&HYr. - 1);
%Let Yr5 = %eval(&HYr. - 4);

%put HYR: &hyr;
%put YR2: &yr2.;
%put YR5: &yr5.;

EDIT: If you were trying to do other calculations that included decimals you would need to use %SYSEVALF instead.

%let HYR = 2018;
%Let Yr2 = %sysevalf(&HYr. - 0.1);
%Let Yr5 = %sysevalf(&HYr. - 0.4);

%put HYR: &hyr;
%put YR2: &yr2.;
%put YR5: &yr5.;
Reeza
  • 20,510
  • 4
  • 21
  • 38