2

I am trying to use substr function in SAS macros like this:

%let hg=Name;
data gg_;
set sashelp.class; 
gh=%substr(&hg,1,3);
run;

and also I tried

data gg_;
set sashelp.class; 
gh=%sysfunc(substr(&hg,1,3));
run;

Here Name is the variable in sashelp.class

But I do not get the first three chars from Name variable into gh. How do I do it?

Llex
  • 1,770
  • 1
  • 12
  • 27
user3658367
  • 641
  • 1
  • 14
  • 30

2 Answers2

1

You are mixing macro and data step logic. Since it is a data step, use the SUBSTR() function, not %substr. If the macro variable consists of the text you want to extract then quote it, otherwise leave the macro variable unquoted.

gh=substr(&hg, 1, 3);

Note: edited to reflect comment.

Reeza
  • 20,510
  • 4
  • 21
  • 38
  • 1
    I think he meant for the macro variable to contain the name of the variable that is to be used in the SUBSTR() function call. So remove the quotes. – Tom Jan 18 '16 at 15:56
0

If you are creating a dataset inside a macro function then you want to use the normal sas function so you just need to use substr() instead of %substr().

%let hg=Name;
data gg_;
set sashelp.class; 
gh=substr(&hg,1,3);
run;
Robert Penridge
  • 8,424
  • 2
  • 34
  • 55
Parul
  • 21
  • 1