DATA Step, Macro, Functions and more

Function Inside String

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

Function Inside String

[ Edited ]

Hi,

 

I would like to use a function which returns a string as a part of another string, without declaring it before.

That's very easy but I don't know

 

It's during an import from Excel:

sheet = "sheet_substrn(&datee.,max(1,length(&datee.)-3),4)"

 

It doesn't understand it need to replace the function with the value.

 

Thanks for answer, and sorry for the basic question.


Accepted Solutions
Solution
‎01-11-2017 10:39 AM
Super User
Posts: 5,081

Re: Function Inside String

[ Edited ]

You will need to switch from SAS language functions to macro language.  For some functions, such as LENGTH, there is already a macro version of the function available (%LENGTH).  But for others (SUBSTR, MAX), where there is no macro version of the function, you will need to apply %SYSFUNC to allow macro language to use the DATA step function. 

 

sheet = "sheet_%sysfunc(substr(&datee.,%sysfunc(max(1,%length(&datee.)-3)),4))"

 

I think I got the parentheses in the right places.  Also note, macro language treats text as character, so you can use SUBSTR instead of SUBSTRN.

View solution in original post


All Replies
Super User
Posts: 5,256

Re: Function Inside String

I'm not sure it's very simple. 

Can you give examples of cell values and the desired outcome in SAS?

Also, please describe why you are doing this.

Data never sleeps
Solution
‎01-11-2017 10:39 AM
Super User
Posts: 5,081

Re: Function Inside String

[ Edited ]

You will need to switch from SAS language functions to macro language.  For some functions, such as LENGTH, there is already a macro version of the function available (%LENGTH).  But for others (SUBSTR, MAX), where there is no macro version of the function, you will need to apply %SYSFUNC to allow macro language to use the DATA step function. 

 

sheet = "sheet_%sysfunc(substr(&datee.,%sysfunc(max(1,%length(&datee.)-3)),4))"

 

I think I got the parentheses in the right places.  Also note, macro language treats text as character, so you can use SUBSTR instead of SUBSTRN.

Contributor
Posts: 45

Re: Function Inside String

Thanks it works like this Smiley Happy

Super User
Posts: 5,081

Re: Function Inside String

Glad it works, but I may have overcomplicated this a bit.  Macro language does contain a %SUBSTR function.

 

Contributor
Posts: 45

Re: Function Inside String

Yes that's a stange solution.

But it's ok if it works, I don't ask for more Smiley Happy Thanks for your help!

 

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 161 views
  • 2 likes
  • 3 in conversation