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