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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 8 replies
  • 1164 views
  • 0 likes
  • 2 in conversation