BookmarkSubscribeRSS Feed
raja777pharma
Fluorite | Level 6

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);

 

 

 

  

 

 

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You need to enclose SUBSTRN (or any data step function) inside %sysfunc

 

%let file_title1 = %sysfunc(substrn(&titles,1,%kfind(&titles,' ')&title1));
--
Paige Miller
Astounding
PROC Star

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;
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @raja777pharma 

 

We need to take this in several steps. 

 

First thing is that your data step doesn't work. There are two errors:

ErikLund_Jensen_0-1624785424383.png

  1. You dont' get a value in title1, because the argument to symget should be quoted like in the line above.
  2. The substrn function takes max. 3 arguments, but you have added title1 as a fourth argument. If you want to concatenate the value of title1 to the substring, you need to use either a concatenation operator or a CAT function.

 

Then I need to ask a question about your wanted output, because when the errors are fixed, the result is:

 

ErikLund_Jensen_2-1624786134286.png

 

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 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 581 views
  • 1 like
  • 5 in conversation