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

Hello here below i have imported a excel sheet which contains certain string i.e  

below is the code i tried can u'll help to correct this code to get my desired output or i use a macro? help! is appreciated....

thanks...

 

data test;
set work.temp;
fullpath=scan(fullpath,-5);
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

In case you want to use a macro like a function, you need to define it differently. But I'd rather use a user-defined data step function instead:


%macro extract_letter(instring);
substr(scan(&instring,-2,'\'),1,1) 
%mend;

proc fcmp outlib=work.funcs.test;
function extract_letter(instring $) $;
  return (substr(scan(instring,-2,'\'),1,1));
endsub;
run;

options cmplib=work.funcs;

data want;
fullpath = 'D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ';
result1 = extract_letter(fullpath);
result2 = extract_letter('D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ');
result3 = %extract_letter(fullpath);
result4 = %extract_letter('D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ');
run;

Note that the results in the dataset from the function will have the desired length of 1, while the results from the macro invocation will default to length 200.

View solution in original post

8 REPLIES 8
RTelang
Fluorite | Level 6
but how would i incorporate this in a macro?
RTelang
Fluorite | Level 6
I have multiple strings like this in my excel sheet so i would just run the macro & display the letter so i asked abt the macro....
Kurt_Bremser
Super User

This is quite easy, then.

%macro extract_letter(varname);
&varname = substr(scan(&varname,-2,'\'),1,1);
%mend;

You just use

%extract_letter(fullpath);

to get the result from your initial example.

RTelang
Fluorite | Level 6
NOTE: Line generated by the macro variable "VARNAME".
'D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx '
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
this error arises when i run the code with a specific address .
Kurt_Bremser
Super User

I guess you still have a severe misunderstanding of the macro language. It is a code generator, not code in itself.

My macro is supposed to be used on a variable name, not on a string literal!

That's why the macro parameter is called varname.

Kurt_Bremser
Super User

In case you want to use a macro like a function, you need to define it differently. But I'd rather use a user-defined data step function instead:


%macro extract_letter(instring);
substr(scan(&instring,-2,'\'),1,1) 
%mend;

proc fcmp outlib=work.funcs.test;
function extract_letter(instring $) $;
  return (substr(scan(instring,-2,'\'),1,1));
endsub;
run;

options cmplib=work.funcs;

data want;
fullpath = 'D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ';
result1 = extract_letter(fullpath);
result2 = extract_letter('D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ');
result3 = %extract_letter(fullpath);
result4 = %extract_letter('D:\Projects\SDTM_Mapping\domains\BD2 - \D - \SDTM-v3.xlsx ');
run;

Note that the results in the dataset from the function will have the desired length of 1, while the results from the macro invocation will default to length 200.

SAS Innovate 2025: Register Now

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1671 views
  • 0 likes
  • 2 in conversation