DATA Step, Macro, Functions and more

Using macro function in DATA step

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Using macro function in DATA step

This works.

 

%put %substr(CHAR_DATA,1,4);

But when used in DATA step it does not work.

data check;
  var=%substr(CHAR_DATA,1,4);
run;
******* or *********;
%macro test;
data check;
  var=%substr(CHAR_DATA,1,4);
run;
%mend test;
%test

 

Thanks for explanation. 


Accepted Solutions
Solution
‎12-07-2015 04:06 PM
Super User
Posts: 5,503

Re: Using macro function in DATA step

Posted in reply to SAS_inquisitive

The statement works, but it doesn't do what you are expecting.  Macro language is not processing your data.  It has an entirely different function.  It generates the SAS language statements that will then process your data.  So this statement is legal:

 

%let newvar = %substr(CHAR_VAR, 1, 4);

 

It gives &NEWVAR the value CHAR (the first four characters in "CHAR_VAR").

 

That means that %SUBSTR works, and gives you a program that looks like this:

 

data check;

var = CHAR;

run;

 

Probably, this is not the program that you intended to generate.  So work backwards.  Start with what you would like to see as a SAS program, and then see where macro language might be able to help you get there.

 

Good luck.

View solution in original post


All Replies
Solution
‎12-07-2015 04:06 PM
Super User
Posts: 5,503

Re: Using macro function in DATA step

Posted in reply to SAS_inquisitive

The statement works, but it doesn't do what you are expecting.  Macro language is not processing your data.  It has an entirely different function.  It generates the SAS language statements that will then process your data.  So this statement is legal:

 

%let newvar = %substr(CHAR_VAR, 1, 4);

 

It gives &NEWVAR the value CHAR (the first four characters in "CHAR_VAR").

 

That means that %SUBSTR works, and gives you a program that looks like this:

 

data check;

var = CHAR;

run;

 

Probably, this is not the program that you intended to generate.  So work backwards.  Start with what you would like to see as a SAS program, and then see where macro language might be able to help you get there.

 

Good luck.

🔒 This topic is solved and locked.

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

Discussion stats
  • 1 reply
  • 154 views
  • 0 likes
  • 2 in conversation