Saturday, June 20, 2009

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.

Friday, June 19, 2009

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.

Saturday, June 13, 2009

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

Friday, June 12, 2009

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












Sunday, June 7, 2009

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

Friday, May 8, 2009

Comparing Two Methods for Removing Formats and Informats in SAS: DATA Step vs. PROC DATASETS

Comparing Two Approaches to Removing Formats and Informats in SAS

Comparing Two Approaches to Removing Formats and Informats in SAS

When working with SAS datasets, there are times when you need to remove formats and informats that have been previously assigned to variables. Two primary approaches can be used for this task:

  • Using the DATA Step
  • Using the PROC DATASETS Procedure

This article compares and contrasts these two approaches to help you determine which method is most appropriate for your needs.

Approach 1: Using the DATA Step

The DATA step is a versatile and commonly used method for removing formats and informats. By assigning variables to a null format or informat, you can effectively remove these attributes from your dataset.

Example:

data mydata_clean;
    set mydata;
    format _all_;
    informat _all_;
run;

In this example, the mydata dataset is processed in the DATA step, and all formats and informats are removed. The resulting dataset mydata_clean is a new dataset without any formats or informats.

Advantages:

  • Flexibility: The DATA step allows you to remove formats and informats from specific variables or all variables in the dataset.
  • Control: You can perform additional data manipulation or transformation while removing formats, all within the same DATA step.
  • Simplicity: The syntax is straightforward and familiar to most SAS users.

Disadvantages:

  • Data Duplication: The DATA step creates a new dataset, which can be inefficient when working with large datasets, as it requires additional storage space.
  • Processing Time: For very large datasets, the process of creating a new dataset can be time-consuming.

Approach 2: Using the PROC DATASETS Procedure

The PROC DATASETS procedure provides another method for removing formats and informats. Unlike the DATA step, this procedure can modify the dataset in place, avoiding the need to create a new dataset.

Example:

proc datasets library=work nolist;
    modify mydata;
    format _all_;
    informat _all_;
    quit;

In this example, the dataset mydata is modified directly in the WORK library. All formats and informats are removed from the dataset without creating a new dataset.

Advantages:

  • Efficiency: Since the dataset is modified in place, this approach can be more efficient in terms of both processing time and storage space.
  • Scalability: PROC DATASETS is well-suited for handling large datasets because it avoids data duplication.
  • Batch Processing: The procedure can be easily integrated into larger batch processes where multiple datasets need to be modified.

Disadvantages:

  • Limited Control: Unlike the DATA step, PROC DATASETS does not allow for additional data transformations or manipulations during the removal of formats.
  • Less Familiarity: Some SAS users may be less familiar with PROC DATASETS, making it slightly less intuitive than the DATA step.

Comparison Summary

Both approaches have their strengths and weaknesses, and the choice between them depends on the specific needs of your task:

  • Use the DATA Step if you need to perform additional data manipulation while removing formats, or if you prefer a method that is simple and easy to understand.
  • Use PROC DATASETS if you are working with large datasets and want to avoid data duplication, or if you need to modify datasets in place for efficiency.

Conclusion

Removing formats and informats is a common task in SAS, and understanding the advantages and limitations of both the DATA step and PROC DATASETS will help you choose the most appropriate method for your specific situation. By mastering both techniques, you can ensure that your data processing tasks are both efficient and effective.

Learn how to view SAS dataset labels without opening the dataset directly in a SAS session. Easy methods and examples included!

Quick Tip: See SAS Dataset Labels Without Opening the Data Quick Tip: See SAS Dataset Labels With...