Hi all, I know I am bit late actually very late in posting this blog post. So, we will be learning SAS today and I am covering some basics of SAS. So, the very first question is What is SAS and SAS software. Want to know about SAS company and SAS programming off lately? We’ll know SAS introduction and then learn creating SAS dataset.
So, in order to learn it first there are multiple things that we will cover. What is SAS and why it is used.
What is SAS and SAS Software:
SAS is a statistical and business analytics package tool that has been developed by SAS Institute of advanced analytics. SAS software helps you derive insights from the data and visualize the output to client. Yeah too much in a statement…?? 😉 No problem we’ll understand all of these things here.
Firstly, let’s check what makes SAS and what are the primary components of it:
Primary components of SAS Programming:
1. SAS Library – This is just a SAS folder where in you will be keeping all your sas dataset or the files you would like your system to be read
2. SAS Editor Section – Here you will be writing your SAS code. SAS coding is more SQL like. If you want to learn SQL also, check my article: Learn Basics of SQL
3. SAS log section – This SAS component will give you all the information regarding the changes your code have made or the information from your imported SAS dataset. For example, the count of observations and rows, the data type of these fields and length. This component is very important to check any issues related to data type issue while performing data merging or transfer from SAS ton Teradata.
4. SAS debug section – This section enables the SAS developer to do any sort of debugging over the issue. There are various options available that lets you check the issue by filtering and sorting record.
How to Create Library in SAS Programming:
– This is the very first step that you will be doing in SAS Enterprise Guide or SAS Basic. There is a default SAS Library called – “WORK” library. This keeps all the temporary data. So, there are two types of libraries:
1. WORK 2. User Defined Library
Work – Keeps all temporary data of that particular SAS session only. Once the system is log off, all the data is erased. So, better you save it in some user defined library by before loosing it…LOL
User Defined Library – You can create library by following the syntax given as following:
Libname [Your Library Name] “Location where you want to save your files or SAS Dataset”
example: Libname AA “C://User/John/Documents/New Datasets/”
This will save all the SAS dataset in New Datasets folder.
Now Learn the three major components of SAS Code:
Steps of SAS Programming:
1. Data Step 2. Proc Step 3. Run Step
So, for today this is all folks to be learnt in basics of SAS Programming :p
Database is an organized collection of related information. In daily world we deal with lots of data. In this internet technology more and more data is getting produced these days. We have multiple database management systems available with us to manage, store and update this enormous data in convenient way. e.g. Oracle, Sybase, Microsoft SQL server management studio etc.
DBMS and SQL
DBMS (Database management server) is a collection of software tools used to manage, update, retrieve the data from the database. SQL (Structured query language is used to connect the DBMS with the database.
All queries have been executed on the Microsoft SQL Server management Studio version 17.0. SSMS is a client tool and not the server. It is rather used as a tool to connect to the database server.
Settings: Local Host
Connect: Database Engine
Use SQL authentication username and password
In Microsoft SQL Server Management Studio you will find two types of databases:
User created Database
-System database can’t be deleted
SQL Command Types:
DDL (Data Definition language) – Used to define/create database object
DML (Data manipulation language) – Used to insert values, update and delete values from the database object created by DDL commands.
TCL (Transaction Control language) – Used to control transactions through Commit and Rollback commands
SQL DDL Commands – data definition language (Create, Alter and Drop commands)
1. Creating a database:
Database can be created either using GUI or through SQL query in SSMS.
Create statement is used for this purpose: Create [Database Object] [Database Object name]
Ex. Create Database db1 (this statement will create a database with name db1)
Whenever we create a database, two types of files are created with it: 1. .MDF file (contains actual data) 2. .LDF file (contains log file)
2. Modify a Database:
Alter statement is used to alter a sql database object.
Alter Command: Alter [Database Object] [Database Object name] Modify Col1 = Value
E.g. Alter Database db1 Modify Name = db2 (this will change the name of the database)
Drop statement is used to delete a database completely from the system(.mdf and .ldf files are also deleted with it)
Drop command: Drop[Database Object] [Database Object name]
e.g. Drop Database db1 (this will delete database db1)
Note- If a database is getting used by any other user, make sure that database is not getting used by any other database. Else an error will be generated
Resolve this single user thing, use this command:
Alter Database db1 set Single_USER with Rollback immediate
(Rollback immediate, rollback any commands and delete the database immediately)
SQL DML Queries : Insert, Update, delete
1. Create a Table:
Command: Create Table [table name] ([column name] [data type of column] [constraint])
e.g. Create table t1(ID int NOT NULL Primary Key, Gender nvarchar(20) NOT NULL)
This command will create a table with name t1 and 2 columns ID and Gender of INT and nvarchar datatypes respectively. nvarchar is a UNICODE data type and store 2 bytes per characters, while varchar stores 1 byte per character.
In order to store the table in a particular database use the following command:
Use [database name]
Create table command….
Primary Key – Can’t be null and must be unique. It uniquely identify each row in the table
Foreign key– It can contain null values and it references primary key present in some other values (basically the column in which it looks for a value). Foreign key is used to establish relationship between two tables. It is used to enforce database integrity.
e.g. Alter table tb1 add constraint tb1_age_check check(AGE>0 AND AGE<30)
This command will only let you add age between 0 and 30 in the Age column.
Note: The check constraint returns a Boolean value based on which the value is entered in the table. It also let you insert Null values because for Null values, check constraint returns “Unknown”.
10. Identity Column:
It is a column property in SSMS.
Identity column is a column to which values are automatically assigned. There are different properties linked to this column. Please set up following values while creating identity column:
Identity Seed: A value with which the identity column value starts
Identity Increment: The value with which identity column value is incremented.
Command: Create table stu(id int identity(1,1) Primary key)
This command will create a stu table having id as a identity column. The id column here will start from 1 and incremented by 1.
10. Setting up External Values/Explicit Value to Identity Column:
To set up external value in Identity column, add the following command before inserting values in table:
Command: Set IDENTITY_INSERT [table name] ON
Insert into table name(column list) values(1,”23″..etc)
11. Setting Off External Values/Explicit Value to Identity Column:
Command: Set IDENTITY_INSERT [table name] OFF
Insert into table name(column list) values(1,”23″..etc)
Note: To reset the identity column value, use DBCC command.
12. Unique Key Constraint:
Unique key constraint is used to enforce unique values in database. There is a slight difference between primary key and unique key.
Primary key values = Unique+Not Null
Unique constraint value = Unique + values can be null
Command: Alter table table_name add constraint constraint_name unique(column name)
Create table Stu(Name varchar(20) Unique)
13. Applying a Trigger:
Firstly let’s try to understand what a trigger is. A trigger is an sql instruction/set of instructions that will will cause an action once a specific condition occurs. For example: Inserting another row table 2 when a row is entered in the table 1.
Create Trigger [trigger_name] on [table_name] for Insert/Update/Delete/Condition
14. Selecting values from table:
Select is a command used to retrieve records from a table.
To fetch all records from a table:
Command: Select * from [table_name]
e.g. Select * from emp
2. Select specific columns from a table:
Command: Select [col_name_1], [col_name_2]… from [table_name]
e.g. Select name, age, id from Employee
3. Fetch all distinct records from a table:
Command: Select distinct [column_name] from [table_name]
e.g. Select distinct name from Employee
This command will help in fetching the distinct records from table Employee by Name column.
4. Fetch record matching a specific condition:
Where is used to apply a specific condition in the SQL command.
Command: Select * from table_name where column_name = condition value
e.g. Select name, id from employee where name=”John”
This command will fetch all the records from the table with name column value as john.
5. Fetch record not matching a specific condition (column value):
Command: Select * from table_name where col_name <> Column value
“<>” signifies as not equal to here. We can also use “!=” to compare values.
6. OR operator in SQL:
OR operator is used to specify two or more conditions together.
Command: Select * from table_name where col1=value OR col2=value
e.g. Select name, age, salary from Employee where name=”John” OR name=”Nick”
This sql command will fetch all the table records where name is either John or Nick
7. AND Operator in SQL:
AND operator is used to specify two and more conditions together.
Command: Select * from table_name where col1=value AND col2=value
e.g. Select name, age, salary from Employee where name=”John” AND age=”30″
This sql command will fetch all the table records where name is John and age is Nick
8. IN Operator in SQL:
IN operator is used to retrieve records where condition matches more than 1 value. (And you don’t want to use OR multiple times in a sql command)
Command: Select * from table_name where col_name IN(value1, value2, value3…)
e.g. Select * from Employee where age(21, 25, 30)
This command will fetch all the table record where age is either 21 or 25 or 30.
SQL supports various kind of wild card characters to facilitate data retrieval in multiple ways. Please refer the image for all sql wild card characters.