DATA Step, Macro, Functions and more

User Define character function

Accepted Solution Solved
Reply
Super Contributor
Posts: 265
Accepted Solution

User Define character function

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;


Accepted Solutions
Solution
‎08-12-2014 09:41 AM
Super User
Super User
Posts: 6,500

Re: User Define character function

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;

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: User Define character function

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.

Super Contributor
Posts: 265

Re: User Define character function

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

Respected Advisor
Posts: 3,777

Re: User Define character function

Use a VALUE format.  Keep the data and the code separate.

Super Contributor
Posts: 265

Re: User Define character function

Thats the second option,  I am looking for function where passing and returning character value. is it possible in sas?

Regular Contributor
Posts: 217

Re: User Define character function

Proc FCMP allows for the creation of user defined functions.

http://support.sas.com/kb/45/243.html

Super Contributor
Posts: 265

Re: User Define character function

If it is, then whats wrong with my func?

Solution
‎08-12-2014 09:41 AM
Super User
Super User
Posts: 6,500

Re: User Define character function

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;

Regular Contributor
Posts: 217

Re: User Define character function

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;

Super Contributor
Posts: 265

Re: User Define character function

Thanks for your guidance.

Super User
Super User
Posts: 7,401

Re: User Define character function

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 328 views
  • 3 likes
  • 5 in conversation