Daniel Robert-Nicoud
Daniel Robert-Nicoud

Reputation: 330

Declaring macro variable with evaluation of function as value

I'm new to SAS. I encurred into a problem when trying to declare a macro variable with the result of some operation as value.

data _null_; %let var1 = 12345; %let var2 = substr(&var1., 4,5); run;

I get that var2 has value substr(&var1., 4,5) (a string) instead of 45 as I would like. How to make the variable declaration evaluate the function?

Sorry it the question is trivial. I looked in the documentation for a bit but couldn't find an answer.

Upvotes: 1

Views: 80

Answers (1)

Amir
Amir

Reputation: 1000

There is a macro equivalent called %substr() which can be used as follows:

%let var1 = 12345;
%let var2 = %substr(&var1., 4,2);

%put var2 = &var2;

Note that the data and run statements are not required for macro language processing and the 3rd argument to %substr() (and substr()) specifies the length you want, not the position of the last character, which is why I used 2 instead of 5.

Edit: Also, if there is no macro equivalent then you could use %sysfunc() to make use of the data step function in macro code. See the documention for full details as there are some quirks, such as not using quotes and a few exceptions to the list of data step functions that can be used.

Upvotes: 1

Related Questions