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

Hi,

 

I have a macro that imports all Excel files in a given directory. The name of each Excel file contains information about year that I want to later use. However, in different files, the position of the year is different, e.g. datafile2016.xlsx, datafile2016_upd.xlsx etc.

I want to create a macro variable that extracts the year from each name of the file. My idea is to use nested substring and index functions:

year=substr(&name, x, 4), where x is the position of the "20" substring (first two digits of the year) that I find by using: index(&name, "20") and &name contains whole name of the file.

 

Since I'm doing this in the macro, I use %sysfunc with each function, which gives me the following code:

 

 

%let year=%sysfunc(%substr(&name, %sysfunc(inputn(%sysfunc(index(&name, "20")), 2.)), 4));

 

However, this produces the following error: ERROR: Function name missing in %SYSFUNC or %QSYSFUNC macro function reference.

 

What am I doing wrong?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

%SYSFUNC is only need to permit macro language to utilize a non-macro function.  The first %SYSFUNC should be removed, since %SUBSTR is a macro function.

 

You may have other issues once this error is gone.  It's likely you need to remove the quotes around "20" since macro language would take that to be a four-character string.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

%SYSFUNC is only need to permit macro language to utilize a non-macro function.  The first %SYSFUNC should be removed, since %SUBSTR is a macro function.

 

You may have other issues once this error is gone.  It's likely you need to remove the quotes around "20" since macro language would take that to be a four-character string.

gamotte
Rhodochrosite | Level 12

Hello,

 

You can extract the year with prxchange as follows :

 

%let a=datafile2016.xlsx;
%let b=%sysfunc(prxchange(s/.*(20\d{2}).*/$1/,-1,&a.));

chris2377
Quartz | Level 8
Thanks. I did both and it works now!
chris2377
Quartz | Level 8

@gamotte

 

Thanks. That's another approach. I promise to myself that one day I'll learn Pearl regular expressions in SAS 🙂 

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
  • 4 replies
  • 1148 views
  • 0 likes
  • 3 in conversation