Hello ,
I would like to use 'SUBSTRN' in macro code , but when use this one gettng errors.
we can use %ksubstr in macro , but how to use 'SUBSTRN' in macro code.
Below is my exist code in data step , but would like to write in macro code to assing macro varible value.
%let titles=listing 16-88.4. many text many text words;
%let title1= Subject listing by devations;
data test;
titles=symget('titles');
title1=symget(title1);
file_title1=substrn(titles,1,find(titles,' ')),title1);
run;
output is = file_title1="listing 16-88.4. Subject listing by devations";
woudl like to write above cod in macro to assing to macor varible like below but i am getting errors, please help me to for this issue, but not use cally symput in data step to create macro varible or proc sql.
%let file_title1=%ksubstrn(&titles,1,%kfind(&titles,' ')&title1);
You need to enclose SUBSTRN (or any data step function) inside %sysfunc
%let file_title1 = %sysfunc(substrn(&titles,1,%kfind(&titles,' ')&title1));
Does the beginning of &TITLES always contain a number in the form ##.#. ? If so, you could use a direct approach:
%let file_title1 = %scan(&titles, 1, .).%scan(&titles, 2, .). &title1;
We need to take this in several steps.
First thing is that your data step doesn't work. There are two errors:
Then I need to ask a question about your wanted output, because when the errors are fixed, the result is:
I expect your wanted output to be the string "listing 16-88.4. Subject listing by devations", but that is not what you get, because you take a substring from start to first blank character, so the number part is omitted. And a move from a data step to macro code wouldn't change that.
It would be easier to forget about substringing and use scan instead, so you can pick the strings you want regardless of length:
%let titles=listing 16-88.4. many text many text words;
%let title1= Subject listing by devations;
%let file_title = %scan(&titles,1,%str( )) %scan(&titles,2,%str( )) &title1;
This gives:
listing 16-88.4. Subject listing by devations
Good luck
Erik
, because the not what you get from your data step. You take a substring from start to first blank character, so the step actually gives
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.