BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aman4SAS
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Aman4SAS
Obsidian | Level 7

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

data_null__
Jade | Level 19

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

Aman4SAS
Obsidian | Level 7

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

jwillis
Quartz | Level 8

Proc FCMP allows for the creation of user defined functions.

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

Aman4SAS
Obsidian | Level 7

If it is, then whats wrong with my func?

Tom
Super User Tom
Super User

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;

jwillis
Quartz | Level 8

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;

Aman4SAS
Obsidian | Level 7

Thanks for your guidance.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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