How to select 2 digits of a 4 digit

Reply
Contributor
Posts: 38

How to select 2 digits of a 4 digit

Hi,

I have a macro reading the Yr in and the Yr is a four digit. How can I select the last 2 digit and put it in another variable like Yr2 that I want to use in the same macro. I tried SUBSTR but it doesnt work.

Thank you.

%macro DATAnSETS(Yr);

/*Yr = 1993 , 1994 , 1995*/

yr2 = SUBSTR(yr,3,2);

Super User
Posts: 5,427

Re: How to select 2 digits of a 4 digit

You are mixing macro and data step syntax. What do you want to accomplish?

%let yr2 = %substr(&yr,3,2);

Data never sleeps
Super User
Super User
Posts: 7,042

Re: How to select 2 digits of a 4 digit

SUBSTR() is a function that can operate on data, but in your code YR is macro variable and not a variable in a SAS dataset.

Plus to reference the value of your macro variable you need to prefix the name with an &.  To assign the value to another macro variable you need to use %LET statement.

You can use the %SUBSTR() function to select part of the string that is the value of macro variable.

%macro test(yr);

  %let yr2=%substr(&yr,3,2) ;

     %put yr2=&yr2 ;

%mend test;

%test(1925)

yr2=25

%test(ABCDEFG);

yr2=CD

Contributor
Posts: 38

Re: How to select 2 digits of a 4 digit

Thank you so much. It worked!

Ask a Question
Discussion stats
  • 3 replies
  • 246 views
  • 0 likes
  • 3 in conversation