AdministratorFebruary 6, 2021 at 11:27 pm
3. IFN and IFC: Conditional Processing: Equivalent to ifelse() function in R.
IFN (logical-expression, value-returned-when-true, value-returned-when-false
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.
commission= IFN (TotalSales > 10000, TotalSales*.05, TotalSales*.02);
IFC: Character Function
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 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.
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');
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…
AdministratorFebruary 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’;
If _N_> 10 then stop;
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
input name $ score score. ;
* informat score score.;
select 'Class 872' as Class_872, avg(score) as AVG_Score
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.