Home Forums Main Forums SAS Forum Advanced SAS Tips

  • Advanced SAS Tips

     Justin updated 7 months, 1 week ago 1 Member · 2 Posts
  • Justin

    Administrator
    February 6, 2021 at 11:27 pm

    3. IFN and IFC: Conditional Processing: Equivalent to ifelse() function in R.

    Syntax
    IFN (logical-expression, value-returned-when-true, value-returned-when-false
    , <,value-returned-when-missing>)

    Details

    The IFN function uses conditional logic that enables you to select among several values based on the value of a logical expression.

    IFN evaluates the first argument, then logical-expression. If logical-expression is true (that is, not zero and not missing), then IFN returns the value in the second argument. If logical-expression is a missing value, and you have a fourth argument, then IFN returns the value in the fourth argument. Otherwise, if logical-expression is false, IFN returns the value in the third argument.

    The IFN function, an IF/THEN/ELSE construct, or a WHERE statement can produce the same results. (See Examples.) However, the IFN function is useful in DATA step expressions when it is not convenient or possible to use an IF/THEN/ELSE construct or a WHERE statement.

    data _null_;
    input TotalSales;
    commission= IFN (TotalSales > 10000, TotalSales*.05, TotalSales*.02);
    put commission=;
    datalines;
    25000
    10000
    .
    0
    663
    .
    500
    ;
    run;

    IFC: Character Function

    Details
    Length of Returned Variable
    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.

    The Basics
    The IFC function uses conditional logic that enables you to select among several values based on the value of a logical expression.

    IFC evaluates the first argument, logical-expression. If logical-expression is true (that is, not zero and not missing), then IFC returns the value in the second argument. If logical-expression is a missing value, and you have a fourth argument, then IFC returns the value in the fourth argument. Otherwise, if logical-expression is false, IFC returns the value in the third argument.

    The IFC function is useful in DATA step expressions, and even more useful in WHERE clauses and other expressions where it is not convenient or possible to use an IF/THEN/ELSE construct.

    data B;
    length name $12 grade 3 performance $30;
    input name $ grade;
    performance = IFC(grade GE 60, 'Pass', 'Fail! Work hard...', 'NA');
    * performance = IFC( substr (name,1,1)='K', 'First name starts with K', 'Others');
    datalines;
    Ted 60
    John 74
    Kareem 89
    . 65
    Jack 53
    Peter .
    Amanda 99
    Smith 0
    . 55
    Sara .
    Kati 35
    Maria 92
    ;
    run;

    Note: In IFN and IFC, a logic expression can have 3 levels:
    1 = true
    0 = false
    (.) = missing when specified.

    You can use functions in the logic expression, such as Substr, Put, Scan etc…

  • Justin

    Administrator
    February 6, 2021 at 11:20 pm

    1. Use SAS ZIP engine to read zip files directly into SAS

    Suppose that you have a ZIP file which contains a lot of different files, and one is called: perfume.log. You can use below code to read it into SAS directly without unzipping it.

    Filename   AAA   SASZIPAM       'C:/users/sas/test.zip' ;
    Filename BBB ‘C:/users/sas/perfume.txt’;
    Data _null_;
    Infile AAA(perfume.log);
    File BBB;
    Input;
    Put _infile_;
    If _N_> 10 then stop;
    Run;

    It is short and simple, but it is perfect and classic coding, and very useful in real work.

    2. Proc Format: Invalue Statement

     proc format fmtlib;
    invalue score notsorted
    'Bottom' = 0
    'Zero'=0
    'Pass'=60
    'Top'=100;
    run;

     data A;
    input name $ score score. ;
    * informat score score.;
    cards;
    Nancy 100
    Amanda 33
    Justin Pass
    Tom Bottom
    Helen Pass
    Peter 77
    June 99
    Ted Zero
    Will Pass
    Derek 71
    ;
    run;
    proc sql;
    select 'Class 872' as Class_872, avg(score) as AVG_Score
    from A;
    quit;

    Note: We can use either ” input name $ score score. ” or “informat score score. ” to read the data in. Alternatively, we can read in Score as char first, then use INPUT() function along with the custom Score. format to convert it into numeric.

Log in to reply.

Original Post
0 of 0 posts June 2018
Now