BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shawn123
Obsidian | Level 7

Hi guys. This is my code, I want to substring the "word" value but the first part of the code cannot work, seems like word resolve into:123456789'. But I don't understand the mechanism behind it?

In the second part, I got the result, but string='5', why it's not '6' when I use the sixth position inside the %substr function?

/********************first part***********************/
%macro createdata(n=,word=); %do i=1 %to &n; data data&i; %if &i>5 %then string=%substr(&word,6); run; %end; %mend; %createdata(n=10,word='123456789');


 

/********************second part***********************/
%macro createdata(n=,word=);
%do i=1 %to &n;
data data&i;
%if &i>5 %then string=%substr(&word,6,1);;
run;
%end;
%mend;
%createdata(n=10,word='123456789');
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

First, there is rarely ever a reason to enclose the values of macro variables in quotes.

 

So instead of

 

%createdata(n=10,word='123456789');

you want

 

%createdata(n=10,word=123456789)

However, you still have a problem here. You state "In the second part, I got the result, but string='5'... "

 

You do not get string='5'. You get string=5. (If you fix it the way @JeffMaggio or I suggest, you get string=6.) In other words string is numeric. Is that what you want?

 

--
Paige Miller

View solution in original post

3 REPLIES 3
JeffMaggio
Obsidian | Level 7

The first character in your string is '
the second character is 1
...

The 6th character is 5

shawn123
Obsidian | Level 7
THANK YOU SO MUCH!
PaigeMiller
Diamond | Level 26

First, there is rarely ever a reason to enclose the values of macro variables in quotes.

 

So instead of

 

%createdata(n=10,word='123456789');

you want

 

%createdata(n=10,word=123456789)

However, you still have a problem here. You state "In the second part, I got the result, but string='5'... "

 

You do not get string='5'. You get string=5. (If you fix it the way @JeffMaggio or I suggest, you get string=6.) In other words string is numeric. Is that what you want?

 

--
Paige Miller