BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sejin
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

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

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

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.

 

kiranv_
Rhodochrosite | Level 12

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);
Sejin
Obsidian | Level 7

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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