DATA Step, Macro, Functions and more

Use of substr function inside macro code

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Use of substr function inside macro code

[ Edited ]

Hello.

 

I am having a problem while writing a macro code. I want to use substr function inside th macro.

Here is an example.

 

%macro name (timej);

 

%let year=substr("&timej",1,4);

proc sql;

create table table2 as

select * from table&year;

quit;

 

%mend;

 

%name (19901231);

 

 

So macro variable timej is 8-digit numeric value which implies year, month, date.

I need to take apart the first 4 digits from the variable.

The above code doesn't work. I tried %substr or other things that I can think of, but never worked properly.

And I want to do it INSIDE the macro code.

 

Could anyone give some advices for this?

 

Thanks for reading my thread.


Accepted Solutions
Solution
‎02-22-2018 08:32 PM
PROC Star
Posts: 549

Re: Use of substr function inside macro code

below code should work.

%macro name (timej);

 

%let year= %sysfunc(substr(&timej,1,4));
%put &year;

proc sql;

create table table2 as

select * from table&year;

quit;

 

%mend;

 

%name(19901231);

View solution in original post


All Replies
Super User
Posts: 2,516

Re: Use of substr function inside macro code

You can either use the macro function

 

%let year=%substr(&timej,1,4);

 

or the sas function via the %sysfunc macro function

 

 

%let year=%sysfunc(substr(&timej,1,4));

 

In any case, no quotes should be used.

 

Solution
‎02-22-2018 08:32 PM
PROC Star
Posts: 549

Re: Use of substr function inside macro code

below code should work.

%macro name (timej);

 

%let year= %sysfunc(substr(&timej,1,4));
%put &year;

proc sql;

create table table2 as

select * from table&year;

quit;

 

%mend;

 

%name(19901231);
Contributor
Posts: 22

Re: Use of substr function inside macro code

Both of you were right. I really appreciate your help. God bless you. Smiley Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 139 views
  • 2 likes
  • 3 in conversation