Discover More Tips and Techniques on This Blog

Showing posts with label New SAS Functions. Show all posts
Showing posts with label New SAS Functions. Show all posts

IFC and IFN functions: New IF functions:

Objective: To Reduce the amount of typing required achieving an objective

Syntax: IFN (condition, true, false, missing): ‘N’ stands for Numeric
IFN returns a numeric value. It returns the true, false or missing value depending on whether the condition is true, false or missing.

Syntax: IFC (condition, true, false, missing): ‘C’ stands for character
IFC function has four parameters:
1) a logical expression
2) character value returned when true
3) value returned when false
4) value retuned when missing, which is optional.

IFC (logical-expression, Character-value-returned-when-true, Character-value-returned-when-false, Character-value-returned-when-missing);
IFC returns a character value. It returns the true, false or missing value depending on whether the condition is true, false or missing.

Example: Assign a value to the VISIT variable (new) as per the VTYPE variable value.
We can certainly achieve this task in diff. ways.. here are they...

data old;
input sitesub $ vtype vdate $;
cards;
01-303 1.4 12/23/2005
01-304 1.5 09/03/2005
01-305 1.4 10/09/2005
01-306 1.5 11/17/2005
01-307 1.5 05/29/2005
01-308 . 04/30/2005
;
run;

1) * Using Proc Format:
proc format;
value vt
1.4='Baseline'
1.5='Retreat'
.='Missing'
;
run;
data new;
set old;
length visit $20;
visit=put(vtype,vt.);
run;

2) *Using the IF-THEN/ELSE statements;
data new;
set old;
length visit $20;
if vtype=1.4 then visit='Baseline';
else if vtype=1.5 then visit='Retreat';
else if vtype=. then visit='Missing';
run;

3) *Using the Proc SQL;
Proc sql;
Create table new4 as
Select *,
case
when vtype=1.4 then 'Baseline'
when vtype=1.5 then 'Retreat'
else 'Missing'
end as visit
from old;
Quit;

All three above methods required significant amount of typing when we compared with the below ones......
*Using the IFC function in Datastep;
data new;
set old;
length visit $20;
visit=ifc(vtype=1.4,'Baseline','Retreat','Missing');
run;


*Using the IFC function in Proc SQl;
Proc sql;
Create table new as
Select *,
Ifc(vtype=1.4,'Baseline','Retreat','Missing') as visit
from old;
Quit;

In a DATA step, if the IFC function returns a value to a variable that has not previously been assigned a length, then that variable is given a length of 200 bytes.

Note: IFN and IFN functions cannot be used if we want to assign more than 3 values (including missing) to a variable.

Concatenation FUNCTIONS & CALL ROUTINES:


  • CATS/CATT/CATX Call Routines
    CAT/CATS/CATT/CATX Functions

    These Functions and Call Routines can be used to join two or more strings together.
    Even though we can use the concatenation operator in combination with the STRIP, TRIM, or LEFT functions, these functions make it much easier to put strings together and if you wish, to place one or more separator characters between the strings.
    One advantage of using the call routines than the functions is improved performance.
    Note: *Call routine executes faster than the function… in specific…

    CALL CATS:
    To concatenate two or more strings, removing both leading and trailing blanks.
    CATS () stands for concatenate and strip.
    Just think the ‘S’ at the end of the CATS as “Strip Blanks”.
    Syntax: CALL CATS (result, string-1<, string-n>);

    Example:

    A=”ABC’;
    B=” CONSULTING”;
    C=”INC “;
    D=” TEAM “;


    FUNCTION= RESULTCALL CATS(RESULT, A,B)= "ABCCONSULTING"
    CALL CATS(RESULT, A,B,C)= "ABCCONSULTINGINC"
    CALL CATS(RESULT, "HELLO",D)= "HELLOTEAM"

  • CALL CATT:
    To concatenate two or more strings, removing only trailing blanks.
    Just think the ‘T’ at the end of the CATT as “Trailing Blanks” or “Trim Blanks”.
    Syntax: CALL CATS (result, string-1<, string-n>);

    Example:
    A=”ABC’;
    B=” CONSULTING”;
    C=”INC “;
    D=” TEAM “;

    FUNCTION= RESULTCALL CATT(RESULT, A,B) ="ABC CONSULTING"
    CALL CATT(RESULT, A,B,C)= "ABC CONSULTINGINC"
    CALL CATT(RESULT, "HELLO",D)= "HELLO TEAM"

  • CALL CATX:To concatenate two or more strings and removing both leading and trailing blanks and places a single space, or one or more characters of our choice, between each strings.
    Just think the ‘X’ at the end of the CATX as “add eXtra blank.”
    Syntax: CALL CATX (separator, result, string-1<, string-n>);
    Example:

    A=”ABC’;
    B=” CONSULTING”;
    C=”INC “;
    D=” TEAM “;

    FUNCTION =RESULT
    CALL CATX(" ",RESULT, A,B) ="ABC CONSULTING"
    CALL CATX(" ,", RESULT, A,B,C) ="ABC,CONSULTING,INC"
    CALL CATX(' / ', RESULT, "HELLO",D) ="HELLO/TEAM"
    CALL CATX(' *** ', RESULT, "HELLO",D) = "HELLO***TEAM"

    Examples:
    CAT:
    The CAT function is identical to the operator
    AE=aeterm aedecod aebodsys;

  • Same result can be achieved using the following code;
    AE=CAT (aeterm, aedecod, aebodsys);
****************************************************************************;

  • CATS:
    AE=trim (left (aeterm)) trim (left (aedecod)) trim (left (aebodsys));

  • Same result can be achieved using the following codes;
    AE=CATT (aeterm, aedecod, aebodsys);
    CALL CATT AE, aeterm, aedecod, aebodsys);
********************************************************************************;

  • CATT:
    AE=trim (aeterm) trim (aedecod) trim (aebodsys);

  • Same result can be achieved using the following codes;
    AE=CATT (aeterm, aedecod, aebodsys);
    CALL CATT (AE, aeterm, aedecod, aebodsys);
    ***************************************************************************;

  • CATX:
    AE=trim (left (aeterm))’/’ trim (left (aedecod)) ’/’ trim (left (aebodsys));

  • Same result can be achieved using the following codes;
    AE=CATX (“ / “,aeterm, aedecod, aebodsys);
    CALL CATX (“ / “, AE, aeterm, aedecod, aebodsys);
    ****************************************************************************;

  • NOTE: Don‘t forget to use the Length statement for the concatenated variable.
Reference: SAS Functions By Example, Chapter1: Character Functions(Page 51-60).

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.