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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.