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');
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?
The first character in your string is '
the second character is 1
...
The 6th character is 5
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?
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.