Hi Can we create user define character function is SAS.
Like:
proc fcmp outlib=sasuser.userfuncs.mymath;
function nm(x);
if x="Anuj" then Return("verma");
Else abort;
endsub;
run;
options cmplib=sasuser.userfuncs;
data _null_;
y=nm("Anuj");
put y;
run;
For your specific application a FORMAT would be preferred.
proc format ;
value $nm 'Anuj' = 'verma';
run;
data _null_;
y=put("Anuj",$nm.);
put y;
run;
You can define functions but you need to tell it the argument and the return values are characters. Also you cannot use ABORT in a function, just return a missing value.
proc fcmp outlib=work.userfuncs.mymath;
function nm(x $) $;
if x="Anuj" then Return("verma");
endsub;
run;
options cmplib=work.userfuncs;
data _null_;
y=nm("Anuj");
put y;
run;
Not sure what the question is, yes you could create a function to do something like that using fcmp. TBH I don't see the value in it though, just use an if or select construct.
I want to create a func where i input a abbrevation of area and get the result of full name of area
like y= areaname("AZ")
then result would be "ARIZONA"
but when i m creating function then i m getting error ,
i tried to find the example for charter func but everywhere example for number.
Can u please suggest on the same,
Thanks
Use a VALUE format. Keep the data and the code separate.
Thats the second option, I am looking for function where passing and returning character value. is it possible in sas?
Proc FCMP allows for the creation of user defined functions.
If it is, then whats wrong with my func?
For your specific application a FORMAT would be preferred.
proc format ;
value $nm 'Anuj' = 'verma';
run;
data _null_;
y=put("Anuj",$nm.);
put y;
run;
You can define functions but you need to tell it the argument and the return values are characters. Also you cannot use ABORT in a function, just return a missing value.
proc fcmp outlib=work.userfuncs.mymath;
function nm(x $) $;
if x="Anuj" then Return("verma");
endsub;
run;
options cmplib=work.userfuncs;
data _null_;
y=nm("Anuj");
put y;
run;
http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002975331.htm
options cmplib = work.funcs;
proc fcmp outlib=work.funcs.mymath;
deletefunc nm;
run;
proc fcmp outlib=work.funcs.mymath;
function nm(x $) $ 5;
if x='Anuj' then Return('verma'); else return('abort');
endsub;
run;
data _null_;
y=nm('Anuj');
put y;
run;
Thanks for your guidance.
And, principally as I don't like formats:
data countries;
length code $2 decode $200;
code="AU"; decode="Austria"; output;
code="UK"; decode="United Kingdom"; output;
...
run;
proc sql;
create table WANT as
select BASE.*,
(select distinct THIS.DECODE from COUNTRIES THIS where THIS.CODE=BASE.CODE)
as COUNTRY
from HAVE BASE;
quit;
You could also do the above by left joining the country data onto the base table.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.