0% found this document useful (0 votes)
9 views

Sas

Uploaded by

aayushs293
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Sas

Uploaded by

aayushs293
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

SAS  Statistical analysis software

Statements can start anywhere and end anywhere. A semicolon at the end of the last line marks the
end of the statement.

Variables  It can be maximum 32 characters long.

The DATA statement marks the creation of a new SAS data set. The rules for DATA set creation are as
below.

A single word after the DATA statement indicates a temporary data set name. Which means the data
set gets erased at the end of the session.

The data set name can be prefixed with a library name which makes it a permanent data set. Which
means the data set persists after the session is over.

# Temporary data sets.

DATA TempData;

DATA abc;

DATA newdat;

# Permanent data sets.

DATA LIBRARY1.DATA1

DATA MYLIB.newdat;
----

 *.sas − It represents the SAS code file which can be edited using the SAS Editor or any text
editor.

 *.log − It represents the SAS Log File it contains information such as errors, warnings, and
data set details for a submitted SAS program.

 *.mht / *.html −It represents the SAS Results file.

 *.sas7bdat −It represents SAS Data File which contains a SAS data set including variable
names, labels, and the results of calculations.

----

* This is comment ;

/*message*/

SAS Variable Types ::

1. Numeric Variables ::
INPUT ID SALARY COMM_PERCENT;
2. Character Variables
INPUT VAR1 $ VAR2 $ VAR3 $;

3. Date Variables

INPUT DOB DATE11. START_DATE MMDDYY10. ;

String Variables ::

data string_examples;
LENGTH string1 $ 6 String2 $ 5;
/*String variables of length 6 and 5 */
String1 = 'Hello';
String2 = 'World';
Joined_strings = String1 ||String2 ;
run;
proc print data = string_examples noobs;
run;
Example
The below code shows how the three types of variables are declared and used in a SAS Program

DATA TEMP;

INPUT ID NAME $ SALARY DEPT $ DOJ DATE9. ;

FORMAT DOJ DATE9. ;

DATALINES;

1 Rick 623.3 IT 02APR2001

2 Dan 515.2 OPS 11JUL2012

3 Michelle 611 IT 21OCT2000

4 Ryan 729 HR 30JUL2012

5 Gary 843.25 FIN 06AUG2000

6 Tusar 578 IT 01MAR2009

7 Pranab 632.8 OPS 16AUG1998

8 Rasmi 722.5 FIN 13SEP2014

PROC PRINT DATA = TEMP;

RUN;

----

SUBSTRN('stringval',p1,p2)

 stringval is the value of the string variable.

 p1 is the start position of extraction.

 p2 is the final position of extraction.

TRIMN('stringval')

Array ::

$  for character values

# Declare an array of length 5 named AGE with values.

ARRAY AGE[5] (12 18 5 62 44);

# Declare an array of length 5 named COUNTRIES with values starting at index 0.

ARRAY COUNTRIES(0:8) A B C D E F G H I;
# Declare an array of length 5 named QUESTS which contain character values.

ARRAY QUESTS(1:5) $ Q1-Q5;

# Declare an array of required length as per the number of values supplied.

ARRAY ANSWER(*) A1-A100;

In the above example all the character variables are declared followed by a $ sign and the date
variables are declared followed by a date format.

we can produce a summary statistics of some of these variables using the Tasks options in SAS
studio. Go to Tasks -> Statistics -> Summary Statistics and double click it to open the window as
shown below.

DATA MYDATA1;

input @1 COL1 4.2 @7 COL2 3.1;

Add_result = COL1+COL2;

Sub_result = COL1-COL2;

Mult_result = COL1*COL2;

Div_result = COL1/COL2;

Expo_result = COL1**COL2;

datalines;

11.21 5.3

3.11 11

PROC PRINT DATA = MYDATA1;

RUN;

^=  Not equal to

SAS loop via do statement ::

DATA MYDATA1;
SUM = 0;
DO VAR = 1 to 5; or DO WHILE(VAR<6) or DO UNTIL(VAR>5);
SUM = SUM+VAR;
END;

PROC PRINT DATA = MYDATA1;


RUN;

DATA EMPDAT;

INPUT EMPID ENAME $ SALARY DEPT $ DOJ DATE9.;

LABEL ID = 'Employee ID';

FORMAT DOJ DATE9.;

DATALINES;

1 Rick 623.3 IT 02APR2001

2 Dan 515.2 OPS 11JUL2012

3 Mike 611.5 IT 21OCT2000

4 Ryan 729.1 HR 30JUL2012

5 Gary 843.2 FIN 06AUG2000

6 Tusar 578.6 IT 01MAR2009

7 Pranab 632.8 OPS 16AUG1998

8 Rasmi 722.5 FIN 13SEP2014

Data EMPDAT1;  ?

Set EMPDAT;  ?

IF SALARY > 650;

OR
IF SALARY > 650 THEN SALRANGE ="HIGH";

ELSE SALRANGE = "LOW";

OR
IF SALARY < 600 THEN SALRANGE = "LOW";

ELSE IF 600 <= SALARY <= 700 THEN SALRANGE = "MEDIUM";

ELSE IF 700 < SALARY THEN SALRANGE = "MEDIUM";

OR
IF SALARY > 700 THEN DELETE;

PROC PRINT DATA = EMPDAT1;

run;

-----

/* Get Maximum value */

max_val = MAX(v1,v2,v3,v4,v5);

/* Get Minimum value */

min_val = MIN (v1,v2,v3,v4,v5);

/* Get Median value */

med_val = MEDIAN (v1,v2,v3,v4,v5);

/* Get a random number */

rand_val = RANUNI(0);

/* Get Square root of sum of the values */

SR_val= SQRT(sum(v1,v2,v3,v4,v5));

Same for lowcase,upcase,reverse

Read File ::

data TEMP;

infile

'/folders/myfolders/sasuser.v94/TutorialsPoint/emp.csv' dlm=",";

input empID empName $ Salary Dept $ DOJ date9. ;

format DOJ date9.;

run;

PROC PRINT DATA = TEMP;

RUN;
Export file ::

PROC EXPORT

DATA = libref.SAS data-set (SAS data-set-options)

OUTFILE = "filename"

DBMS = identifier LABEL(REPLACE);

proc export data = sashelp.cars

outfile = '/folders/myfolders/sasuser.v94/TutorialsPoint/car_data.txt'

dbms = dlm;

delimiter = ' ';

run;

Concatenate dataset ::
DATA ITDEPT;

INPUT empid name $ salary DOJ date9. ;

DATALINES;

1 Rick 623.3 02APR2001

3 Mike 611.5 21OCT2000

6 Tusar 578.6 01MAR2009

RUN;

DATA NON_ITDEPT;

INPUT empid name $ salary ;

DATALINES;

2 Dan 515.2

4 Ryan 729.1

5 Gary 843.25

7 Pranab 632.8

8 Rasmi 722.5

RUN;

DATA All_Dept;
SET ITDEPT NON_ITDEPT;

RUN;

PROC PRINT DATA = All_Dept;

RUN;

Merging ::

DATA SALARY;

INPUT empid name $ salary ;

DATALINES;

1 Rick 623.3

2 Dan 515.2

3 Mike 611.5

4 Ryan 729.1

5 Gary 843.25

6 Tusar 578.6

7 Pranab 632.8

8 Rasmi 722.5

RUN;

DATA DEPT;

INPUT empid dEPT $ ;

DATALINES;

1 IT

2 OPS

3 IT

4 HR

5 FIN

6 IT

7 OPS

8 FIN
;

RUN;

DATA All_details;

MERGE SALARY DEPT;

BY (empid);

RUN;

PROC PRINT DATA = All_details;

RUN;

Below code for only matched data ::

DATA All_details;

MERGE SALARY(IN = a) DEPT(IN = b);

BY (empid);

IF a = 1 and b = 1;

RUN;

PROC PRINT DATA = All_details;

RUN;

Sorting data ::

DATA Employee;

INPUT empid name $ salary DEPT $ ;

DATALINES;

1 Rick 623.3 IT

2 Dan 515.2 OPS

3 Mike 611.5 IT

4 Ryan 729.1 HR

5 Gary 843.25 FIN

6 Tusar 578.6 IT

7 Pranab 632.8 OPS

8 Rasmi 722.5 FIN


;

RUN;

PROC SORT DATA = Employee OUT = Sorted_sal ;

BY salary;

RUN ;

PROC PRINT DATA = Sorted_sal;

RUN ;

data employees(drop = Type);

length Type $ 3 Department

empID $ 3 empName $ 10 Empsal 3 ;

retain Department;

infile (read file)

'/folders/myfolders/TutorialsPoint/empdtls.txt' dlm = ':';

input Type $ @;

if Type = 'DEP' then

input Department $;

else do;

input empID empName $ Empsal ;

output;

end;

run;

PROC PRINT DATA = employees;

RUN;

PROC EXPORT
It is a SAS inbuilt procedure used to export the SAS data sets for writing the data into files of
different formats.

proc export data = sashelp.cars

outfile = '/folders/myfolders/sasuser.v94/TutorialsPoint/car_data.txt'

dbms = dlm;

delimiter = ' ';

run;

same for csv migrate

Concatenate data sets(not variable,however it will be concatenated on the variables itself) ::

DATA All_Dept;

SET ITDEPT NON_ITDEPT;

RUN;

For merging ::

DATA All_details;

MERGE SALARY DEPT;

BY (empid);

RUN;

PROC PRINT DATA = All_details;

RUN;

Subsetting data set ::

DATA OnlyDept;

SET Employee;

KEEP ename DEPT;

DROP salary;

RUN;
PROC PRINT DATA = OnlyDept;

RUN;

Sorting data ::

PROC SORT DATA = Employee OUT = Sorted_sal ;

BY salary;

RUN ;

PROC PRINT DATA = Sorted_sal;

RUN ;

PROC SQL;

SELECT Columns

FROM TABLE

WHERE Columns

GROUP BY Columns

QUIT;
SAS SQL :::

The basic syntax for using PROC SQL in SAS is −

PROC SQL;

SELECT Columns

FROM TABLE

WHERE Columns

GROUP BY Columns

QUIT;

SQL Create Operation ::

DATA TEMP;

INPUT ID $ NAME $ SALARY DEPARTMENT $;

DATALINES;

1 Rick 623.3 IT

2 Dan 515.2 Operations

3 Michelle 611 IT

4 Ryan 729 HR

5 Gary 843.25 Finance

6 Nina 578 IT

7 Simon 632.8 Operations

8 Guru 722.5 Finance

RUN;

PROC SQL;

CREATE TABLE EMPLOYEES AS

SELECT * FROM TEMP;

QUIT;
PROC PRINT data = EMPLOYEES;

RUN;

SQL Read Operation ::

PROC SQL;

SELECT make,model,type,invoice,horsepower

FROM

SASHELP.CARS

QUIT;

SQL UPDATE Operation

DATA TEMP;

INPUT ID $ NAME $ SALARY DEPARTMENT $;

DATALINES;

1 Rick 623.3 IT

2 Dan 515.2 Operations

3 Michelle 611 IT

4 Ryan 729 HR

5 Gary 843.25 Finance

6 Nina 578 IT

7 Simon 632.8 Operations

8 Guru 722.5 Finance

RUN;

PROC SQL;

CREATE TABLE EMPLOYEES2 AS

SELECT ID as EMPID,

Name as EMPNAME ,

SALARY as SALARY,
DEPARTMENT as DEPT,

SALARY*0.23 as COMMISION

FROM TEMP;

QUIT;

PROC SQL;

UPDATE EMPLOYEES2

SET SALARY = SALARY*1.25;

QUIT;

PROC PRINT data = EMPLOYEES2;

RUN;

SQL DELETE Operation

The delete operation in SQL involves removing certain values from the table using the SQL DELETE
statement. We continue to use the data from the above example and delete the rows from the table
in which the salary of the employees is greater than 900.

PROC SQL;

DELETE FROM EMPLOYEES2

WHERE SALARY > 900;

QUIT;

PROC PRINT data = EMPLOYEES2;

RUN;

For mean ::

PROC MEANS DATA = sashelp.CARS mean SUM MAXDEC=2 ;

var horsepower invoice EngineSize;

RUN;

For class

PROC MEANS DATA = sashelp.CARS mean SUM MAXDEC=2;

class make type;

var horsepower;

RUN;
For standard deviation ::

PROC SQL;

create table CARS1 as

SELECT make, type, invoice, horsepower, length, weight

FROM

SASHELP.CARS

WHERE make in ('Audi','BMW')

RUN;

proc means data = CARS1 STD;

run;

Regression ::

PROC SQL;

create table CARS1 as

SELECT invoice, horsepower, length, weight

FROM

SASHELP.CARS

WHERE make in ('Audi','BMW')

RUN;

proc reg data = cars1;

model horsepower = weight ;

run;
Target  Identify value to be changed
Define macro variable  %let firnum = 773

Calling and print macro variable  %put &firnum.;

You might also like