Discover More Tips and Techniques on This Blog

%EVAL AND %SYSEVALF MACRO FUNCTIONS: GETTING TO KNOW THEM BETTER.

With the exception of %sysevalf function, integer arithmetic is the only way macro statements perform arithmetic calculations.
Following are the few examples of macro statements performing integer arithmetic calculations:
%let one=%eval (3+5);
%let two=%eval (5*2);
%let three=%eval (9/3);
%let four=%eval (5/2);
%put The value of one is &one;
%put The value of two is &two;
%put The value of three is &three;
%put The value of four is & four;

Open the Log file and see the results as follows:
The value of one is 8
The value of two is 10
The value of three is 3
The value of four is 2
The value for macro variable four, should be 2.5, instead it shows only two. That happens because if we perform division on integers, integer arithmetic doesn’t take the fractional part into account.
When we try to execute the integer arithmetic calculations of values with functional part, :

%let last= %eval (5.0+3.0); /*INCORRECT*/

%EVAL function only supports integer arithmetic values. The values here in the above statement have a period character to numeric values and because of that the macro processor stops evaluating and produces the following error message: “ ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 5.0+3.0 “

So our next question comes is… how to perform these type of calculations:
Here comes %sysevalf to rescue you, because this function is capable of evaluating the floating point operands.

Evaluating Floating Point Operands
The %SYSEVALF function can perform arithmetic calculations with operands that have the floating point values.
Here are some of the examples where %SYSEVALF function becomes handy:
%let test= %sysevalf(1.0*3.0);
%let final= %sysevalf(1.5+2.8);
%let last= %sysevalf(5/3);
%put The value of test is &test;
%put The value of final is &final;
%put The value of last is &last;
The %PUT statements display the following messages in the log:
The value of test is 3
The value of final is 4.3
The value of last is 1.66666666666666
%SYSEVALF function perform arithmetic calculations and the result of the evaluation can be a floating point value like in the final and last macro variable case, but as in integer arithmetic calculations, the result is always a text.
The %SYSEVALF function be used in conjugation with other functions like, INTEGER, CEIL, and FLOOR.
For example, the following %PUT statements return 3, 4 and 3 respectively:
%let val=3.8;
%put %sysevalf(&val,integer);
*Value returns in the log is 3;
%put %sysevalf(&val, ceil);
*Value returns in the log is 4;
%put %sysevalf(&val,floor);
*Value returns in the log is 3;

Difference between %eval and %sysevalf functions can be understand better with the following example;
%let value=9;
%let value2=5;
%let newval=%sysevalf(&value/&value2);
%let newval1=%eval(&value/&value2);
%put &newval;
%put &newval1;
*Ans: newval=1.8;
*Ans: newval1=1;

DEXPORT and DIMPORT: DISPLAY MANAGER commands used to IMPORT and EXPORT the Tab delimited (Excel and .CSV) files;

One of my favorite methods of exporting excel or .csv file is to use the ‘DEXPORT’ command-line command. This certainly reduces the amount of typing required to export the SAS dataset. Another interesting point is DEXPORT command works fine in UNIX and PC.

 
Syntax: dmDEXPORT libref.dsn 'filename.xls' replace;
 
"libref" is a library, "dsn" is the name of a SAS data set, and "filename.xls" is the name of the 
tab delimited text file(excel) being created. If we don’t specify the Libname or it is work then 
the dataset ‘dsn’ from the WORK directory is exported in a excel format to a specified location. 
Replace option … replaces the file if it already exists. 
 

Use DIMPORT command-line command to convert/import a tab delimited (excel or .csv etc) into a
SAS dataset.
Syntax: dm “DIMPORT ‘filename.csv’ exc" replace;



DIMPORT command tells SAS to import or convert the tab delimited file (filename.csv) to a SAS
dataset named ‘exc’; Replace option … replaces the dataset named ‘exc’ if it already exists by
any chance.

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.

How to Play BlackJack, Solitaire and Poker Games using the SAS software

Here is how we can play BlackJack, Solitaire and Poker etc games using the SAS software.
I bet most people Don't know about this........
Open the SAS session, GO TO Solutions > Accessories > Games

and select any Game ... and enjoy....

I don't have any IDEA, Why SAS included Games in the Software.

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).

Self Teach SAS tutorials




SAS Tutorials
The following are a series of self-teach tutorials which will introduce you to the command language approach to SAS programming. Each tutorial is designed to take about twenty minutes. To work through the tutorials you need to be on a machine which has SAS Version 8 loaded on it, or has access to SAS through the campus network. The material presented in the tutorials is designed to get you 'up and running.' Consequently, many options are omitted. Check the SAS help files for more a detailed discussion of the procedures outlined in the tutorials.

The last tutorial is an introduction to SAS Analyst which is SAS's version of a 'point and click' approach to statistical analysis.


Starting SAS under Windows
Tutorial 1: Data Steps Part 1
Tutorial 2: Data Steps Part 2
Tutorial 3: Data Transformation and Selection
Tutorial 4: Displaying the Data
Tutorial 5: Some Basic Statistical Procedures
SAS Analyst tutorial--interactive SAS
SAS Multilevel Interactive



direct link: http://instruct.uwo.ca/sociology/300a/SASintro.htm












Clean-Up: Delete datasets in the work library:

It is better always to clean-up/empty the work directory before we run the next set of SAS code. This is VERY helpful in situations where the “working” files created tend to use up a large amount of memory, once the logic of the program has been checked, KILLing the working files will result in a more efficient program. Another important reason to issue the above statement at the end of a program is when programs are run in batch, this will clean up the working library to be sure any “old” files are not left around to be erroneously used1.

PROC DATASETS procedure offers an elegant solution to do just.
Remember that there is no need of knowing any dataset names when we are emptying the work directory.

Here is the simple syntax:
proc datasets lib=work kill nolist memtype=data;
quit;

We have specified lib=work, because we are cleaning up the work directory.
KILL option removes all the datasets that are happened to be in the work directory.
NOLIST option tells SAS, printing the details isn’t required.
Since our main objective here to remove the datasets and not the catalogs, we need to specify MEMTYPE=DATA.
To remove datasets and catalogs…use. MEMTYPE=ALL.
What if we need to delete only some datasets and not all….
Proc DATASETS procedure has a solution for it also….datasets..
The following code only deletes 2 datasets, named AE and DEMO.
proc datasets lib=work nolist;
delete AE DEMO;
quit;
run;

CAUTION:
The KILL option deletes the SAS files immediately after you submit the statement.
Source: sugi28 proceedings..page 190-28.pdf

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.