Reputation: 391
I have variable x = 2001. I want to produce a string that looks like this: "TEST0106.xls". The 01 in that string is the last two digits of 2001, and the 06 is the last two digits of 2006 (x+5).
My current code is this:
%let x = 2001;
%let sub1 = %sysfunc(mod(&x, 100));
%let sub2 = %sysfunc(mod(&x+5, 100));
%let test = TEST&sub1&sub2.xls;
%put &test;
However, this just gives me "TEST16xls" since the 0 disappears in the modulus division and I'm not sure why the period isn't there. I believe I'll have to do some approach where I convert the numbers into characters and do a sub-string. How would I go about this task?
Upvotes: 1
Views: 81
Reputation: 63424
First, you don't need to use modulo arithmetic to do a substring in macro variable land, everything is text, so just use %substr
.
Second, you can give an optional argument to %sysfunc
telling it how to format the result; use the z2
format to tell it what you want. I think leaving the modulo is convenient here simply because it does give you that option. Otherwise you can use %sysfunc(putn(
if you want to not use modulo.
Third, you need another .
since the first one ends the macro variable (technically macro variables are &
to .
inclusive unless they hit another character that is invalid for macro variable names first).
%let x = 2001;
%let sub1 = %substr(&x,3,2);
%let sub2 = %sysfunc(mod(&x+5, 100),z2.);
%let test = TEST&sub1.&sub2..xls;
%put &test;
or
%let sub2 = %sysfunc(putn(&sub1.+5,z2.));
Upvotes: 3