DATA Step, Macro, Functions and more

How to call a function that exists a value in a variable.

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

How to call a function that exists a value in a variable.

Hi,

 

data data1;

input val_pas $20. ;

infile datalines truncover;

datalines;

substring\_'

;

run;

 

data data2;

input func $60. ;

infile datalines truncover;

datalines;

prxchange('s/[^0-9a-zA-Z.&_,\\\-\''\[\]\(\)\s]/^/', -1, val_pas)

;

run;

data output;

set data1 data2;

out_value=func;

run;

******************************************************************************

I want to call prxchange function and assign the out put to variable "out_value". For this function  "val_pas" is one of the parameter(data1=Source text for regular expresion matching).

 

Is there any way to execute the function ?

 

Many thanks.

 

Regards

Hari.


Accepted Solutions
Solution
‎05-18-2016 12:11 PM
Super User
Super User
Posts: 7,392

Re: How to call a function that exists a value in a variable.

Yes, you can create code using the call execute function:

data _null_;
  set data2;
  call execute('data output; set data1; out_value='||strip(func)||'; run;');
run;

You will need to read up on the call execute function, it is very powerfull, but you do need to know how to use it.

View solution in original post


All Replies
Solution
‎05-18-2016 12:11 PM
Super User
Super User
Posts: 7,392

Re: How to call a function that exists a value in a variable.

Yes, you can create code using the call execute function:

data _null_;
  set data2;
  call execute('data output; set data1; out_value='||strip(func)||'; run;');
run;

You will need to read up on the call execute function, it is very powerfull, but you do need to know how to use it.

Contributor
Posts: 36

Re: How to call a function that exists a value in a variable.

Hi Super User,

Thanks for your answer. But I am getting an unknown error as below.

+ data output; set data1; out_value=prxchange('s/[^0-9a-zA-Z.&_,\-\''\[\]\(\)\s]/^/', -1, val_pa; run;
_
79

ERROR 79-322: Expecting a ).
Super User
Posts: 5,071

Re: How to call a function that exists a value in a variable.

That error is related to a flaw in your original program.  The length of $60 is not long enough to hold the value that you are trying to read in.  Correct the length and try again.

Contributor
Posts: 36

Re: How to call a function that exists a value in a variable.

Hi RW9,

Could you please provide some links to read about Call Execute function.
Super User
Super User
Posts: 7,392

Re: How to call a function that exists a value in a variable.

Well, top three google responses:

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm

http://www2.sas.com/proceedings/sugi22/CODERS/PAPER70.PDF

http://www2.sas.com/proceedings/sugi22/CODERS/PAPER86.PDF

 

Once you understand the concept its pretty simple and powerful.  Basically the call execute takes a string, which inserted into the code stream after the datastep executing has finished.  That text string will be considered code and compiled as any other code.

Contributor
Posts: 36

Re: How to call a function that exists a value in a variable.

Thank you so much Smiley Happy

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Respected Advisor
Posts: 3,777

Re: How to call a function that exists a value in a variable.

While the PRXCHANGE function does need to be compiled (i.e. can't be an expression) the arguments to the function can be expressions.  

 

http://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#n0r8h2fa8dj...

Super User
Posts: 5,071

Re: How to call a function that exists a value in a variable.

I believe that this is one of the uses for the RESOLVE function:

 

out_value = resolve(func);

 

It's untested, but I think it does what you're asking.

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 334 views
  • 0 likes
  • 4 in conversation