executing macro inside a data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

executing macro inside a data step

Hi,

 

I am executing a macro inside a data step and passing the data step variable to the macro.

 

But only last record variable is passed all the time (like my loop executed 2 times and 2 times last record variable only passed as a parameter).

 

The output should be 

APPDIRECT_PROD_2018011001.dat|20180110|20180110|0000000001|APPDIRECT_PROD_2018011002.|ctl

APPDIRECT_PROD_2018011002.dat|20180110|20180110|0000000002|APPDIRECT_PROD_2018011002.|ctl

 

But I am getting

 

APPDIRECT_PROD_2018011002.dat|20180110|20180110|0000000002|APPDIRECT_PROD_2018011002.|ctl

APPDIRECT_PROD_2018011002.dat|20180110|20180110|0000000002|APPDIRECT_PROD_2018011002.|ctl

 

Where exactly the issue?

 

Thanks,

Swathi


Accepted Solutions
Solution
‎01-11-2018 03:35 PM
Super User
Posts: 8,589

Re: executing macro inside a data step

Posted in reply to swathiprasad

Don't hand the name of a data step variable to call execute, but it's values:

x = cats('%nrstr(%b('!!trim(id)!!'))');
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 8,589

Re: executing macro inside a data step

Posted in reply to swathiprasad

You are repeatedly creating macro variable new with call symput.

You are pushing macro calls onto the execution stack that contain the macro variable name, not it's value.

Since the macros won't execute before the data step has finished, all macro calls will use the last value put into macro variable new.

 

Instead of setting macro variables, use data step values in the call execute.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 15

Re: executing macro inside a data step

Posted in reply to KurtBremser

Thank you Sir,for your reply.

 

I changed the code to  the variable directly,instead of the macro variable as i don't want to hardcode the valueas below.

x = cats( '%nrstr(%b(id))' ) ;

 

then the output is as below.

1 + %b(id)
id
2 + %b(id)
id

 

instead of the resolved variable value.

 

Is there any approach to achieve this?

 

Thanks!!

 

 

Solution
‎01-11-2018 03:35 PM
Super User
Posts: 8,589

Re: executing macro inside a data step

Posted in reply to swathiprasad

Don't hand the name of a data step variable to call execute, but it's values:

x = cats('%nrstr(%b('!!trim(id)!!'))');
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 15

Re: executing macro inside a data step

Posted in reply to KurtBremser

Thank you sir!!

Super User
Super User
Posts: 7,388

Re: executing macro inside a data step

Posted in reply to swathiprasad

So let's eliminate the complexity of your data step from this problem and concentrate on just the CALL EXECUTE issue.

So first create your dataset and then use a DATA _NULL_ step to generate the macro calls.

%macro a ;
data FILELIST ;
  ....
  output;
  ....
run;

data _null_;
  set FILELIST;
  call execute(cats('%nrstr(%b)(',id,')'));
run;
%mend a;

Now you can also probably make your data step much simpler (why are you calling CATT() function on string literals?), but perhaps you can leave that for another question.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 177 views
  • 0 likes
  • 3 in conversation