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;
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.
@RTelang wrote:
but how would i incorporate this in a macro?
Why would you want to do that?
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.
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.
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.
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 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.
Ready to level-up your skills? Choose your own adventure.