Sie sind auf Seite 1von 163

23/08/11 SAS

A Complete programming language with report formatting with statistical and mathematical capabilities. 1st Part.. The first part called the DATA step, This part describes your input data allows you to modify that data with arithmetic operations and logical decisions. The DATA Step will format your data into a special file called SASdataset. The second part of SAS is a library of canned routines called PROCEDURES. The procedures can only use SAS datasets as input. The Data step must always have preceded the Procedure section. The procedures are executed by coding a special statement in SAS called a PROC statement . The core of SAS language:

a programming language that you use to manage your data. tools for data analysis and reporting. a tool for extending and customizing SAS software programs and for reducing text in your programs. tool that helps you find logic problems in DATA step a system that delivers output in a variety of easy-to-access formats, such as SAS data sets, listing files, or Hypertext Markup Language an interactive, graphical user interface that enables you to easily run and test your SAS programs.

A DATA step consists of a group of statements in the SAS language that can read data from external files write data to external files read SAS data sets and data views create SAS data sets and data views. A group of procedure statements is called a PROC step. SAS procedures analyze data in SAS data sets to produce statistics, tables, reports, charts, and plots, to create SQL queries, and to perform other analyses and operations on your data. They also provide ways to manage and print SAS files SAS Macro Facility Base SAS software includes the SAS Macro Facility, a powerful programming tool for extending and customizing your SAS programs, and for reducing the amount of code that you must enter to do

common tasks. Macros are SAS files that contain compiled macro program statements and stored text. You can use macros to automatically generate SAS statements and commands, write messages to the SAS log, accept input, or create and change the values of macro variables.

SAS ARCHITECTURE

SAS Tier - Application Layer: All the Servers Metadata, Workspace Server, Stored Process Server and OLAP (SAS ETL Processes) Mid Tier - Data Management Layer: All Data management and Reporting Services and Tools. (BaseSAS, IOM) Client Tier - Presentation Layer: All Easy (GUI) Reporting Components (SASEG, SAS Miner etc)

Examples: data x; input name $ age sal; cards; prasad 30 20000 venkat 40 60000 ramu 50 80000 run; proc sql;

create table x1 (name char, age int, sal int); quit; proc sql; insert into x1 values('prasad1',30,3000) values('prasad2',40,4000) values('prasad3',50,500000); insert into x1 set name='venkat', age=20, sal=10000 set age=80,name='prasad5',sal=80000; quit; proc freq data=x1;run; Reading Raw Data from External Files Statement: data y; infile 'C:\Users\home\Desktop\ukbabau.txt'; input a b c; run; proc means data=y; run; INPUT STYLES 1.LIST INPUT Data vaues are separated by atleast one blank space.any character values does not have any embded spaces. Example: DATA LISTINP; INPUT ID HEIGHT WEIGHT GENDER $ AGE; DATALINES; 1 68 144 M 23 2 78 202 M 34 3 62 99 F 37 4 61 101 F 45 ; run; data x; input name $1-18 age sal; cards;

ramuso munamrayana 20 30000 meenahi sinha 30 60000 run; 2.COLUMN INPUT In raw data , the data values are availble in specific columns. Statement: input pid 1-3 name $7-17 age 20-21 color $25-29; cards; 100 kiran kumar 89 white 101 pavan 67 black 102 kranthi 89 white run; Example DATA COLINPUT; INPUT ID 1 HEIGHT 2-3 WEIGHT 4-6 GENDER $ 7 AGE 8-9; DATALINES; 168144M23 278202M34 362 99F37 461101F45 ; run; 3.FORMAT INPUT In format input method 2 symbols playing a main role. +n,n. n indicates number @n column pointer n. column range data forin; input @1 pid 3. @7 name $10. @20 age 1. @25 color $4.; cards; 100 kiran kumar 89 white 101 pavan 67 black 102 kranthi 89 white run; 3.format input In format input method 2 symbols plays main role.

+n,n. n indicates number +n column pointer n. column range syntax: data forin; input @1 pid 3. @7 name $10. @20 age 1. @25 color $4.; cards; 100 kiran kumar 89 white 101 pavan 67 black 102 kranthi 89 white run; 4.absolute input Using this method we can read standatrd data and non standard data. @n indicate column hold pointer n. column range syntax: data absol; input @1 pid 3. @7 name $10. @20 age 2. @25 color $4.; cards; 100 kiran kumar 89 white 101 pavan 67 black 102 kranthi 89 white run; 5.mixed input if we use or write one or more input techniques in required input statement called mixed input. data mixed; input pid 3. name $7-17 @20 age 2. @25 color $4.; cards; 100 kiran kumar 89 white 101 pavan 67 black 102 kranthi 89 white run; 6.named input Sometimes data values available with variable name. In this we are using named input. data named; input name=$ age= sal=; cards;

name=prasad age=20 sal=3000 name=giri age=30 sal=4000 age=40 sal=50000 name=ramu run;

Using infile options


Using infile options we can read the data in proper order. DLM: It is used to indicate delimiters in raw data. DATA COMMAS; INFILE DATALINES DLM=',' ; INPUT ID HEIGHT WEIGHT GENDER $ AGE; DATALINES; 1,68,144,M,23 2,78,202,M,34 3,62,99,F,37 4,61,101,F,45 ; DATA COMMAS3; INFILE datalines DLM=', & $' ; INPUT ID HEIGHT WEIGHT GENDER $ AGE; DATALINES; 1,68&144,M,23 2,78,202,M,34 3$62,99,F,37 4,61,101$F,45 ; run; /*Missover*/ If any value is missing in raw data it tries to read next data value.To avoid that behaviour we can use missover. data mis; infile cards missover ; input sal age name $; cards; 2000 20 ramu 3000 40 5000 60 mahesh 6000 65 4000 55 rani run; proc print;run;

/*dsd*/ 1.In raw data, data values are separated by commas we will use dsd. 2.SAS treats missing value in between two consuective delimitres. 3.quotation marks in data values. DATA ds; INFILE DATALINES dsd ; INPUT ID HEIGHT WEIGHT GENDER $ AGE; DATALINES; 1,68,144,M,23 2,78,202,M,34 3,62,99,F,37 4,61,101,F,45 ; DATA ds1; INFILE DATALINES dsd ; INPUT ID HEIGHT WEIGHT GENDER $ AGE; DATALINES; 1,68,144,M,23 2,,202,M,34 3,62,99,F,37 4,,101,F,45 ; proc print;run; data scores; infile datalines dsd; input Name : $9. Score Team : $25. Div $; datalines; Joseph,76,"Red Racers, Washington",AAA Mitchel,82,"Blue Bunnies, Richmond",AAA Sue Ellen,74,"Green Gazelles, Atlanta",AA ; /*Truncover*/ It works like a missover.it adjust vary of the length for required variable & save the diskplace &reduce the storage place. or It manage variable length of variable values. data tr; infile cards truncover; input age sal name $20.; cards; 12 3000 ramash 23 4000 uiytrrewsdfg 45 5690 ramashankarrajutyrewyuu 38 5000 uiopuytrd ;

run; proc print;run; /*scanover*/ used for scaning the string whenever the string starts there it reads the values. data sca; infile cards scanover; input @'ab' name $ loc $ sal; cards; ab appolo hyd 20000 ab image hyd 30000 appolo sec 40000 care ban 50000 care hyd 60000 run; proc print;run; data sca1; infile cards scanover; input @ 'ab' a b ; cards; 10 20 30 30 70 ab 78 89 90 40 ab 49 80 run; proc print;run; /*stopover*/ when error is occured in raw data in reading,stopover tries to stops the reading. data st; infile cards stopover; input t1 t2 t3 t4 t5; cards; 10 22 33 88 99 44 55 99 88 77 66 60 run; DATA LISTINP; INPUT ID HEIGHT WEIGHT GENDER $ AGE; DATALINES; 1 68 144 M 23 2 78 202 M 34 3 62 99 F 37

4 61 101 F 45 ; PROC PRINT; RUN; *---------------------------------------------; *--------------- EXAMPLE 2.1 ----------------; DATA COMMAS; INFILE DATALINES DLM=','; INPUT ID HEIGHT WEIGHT GENDER $ AGE; DATALINES; 1,68,144,M,23 2,78,202,M,34 3,62,99,F,37 4,61,101,F,45 ; PROC PRINT; RUN; *---------------------------------------------; *--------------- EXAMPLE 2.2 ----------------; DATA COMMAS; INFILE DATALINES DSD; INPUT X Y TEXT; DATALINES; 1,2,XYZ 3,,STRING 4,5,"TESTING" 6,,"ABC,XYZ" ; PROC PRINT; RUN; *---------------------------------------------; *---------------- EXAMPLE 3.1 --------------------; DATA INFORMS; INFORMAT LASTNAME $20. DOB MMDDYY8. GENDER $1.; INPUT ID LASTNAME DOB HEIGHT WEIGHT GENDER AGE; FORMAT DOB MMDDYY8.; DATALINES; 1 SMITH 1/23/66 68 144 M 26 2 JONES 3/14/60 78 202 M 32 3 DOE 11/26/47 62 99 F 45 4 WASHINGTON 8/1/70 66 101 F 22 ;

PROC PRINT; RUN; *-------------------------------------------------; *---------------- EXAMPLE 3.2 --------------; DATA COLONS; INPUT ID LASTNAME : $20. DOB : MMDDYY8. HEIGHT WEIGHT GENDER : $1. AGE; FORMAT DOB MMDDYY8.; DATALINES; 1 SMITH 01/23/66 68 144 M 26 2 JONES 3/14/60 78 202 M 32 3 DOE 11/26/47 62 99 F 45 4 WASHINGTON 8/1/70 66 101 F 22 ; PROC PRINT; TITLE 'Example 3.2'; RUN; *--------------------------------------------; *--------------- EXAMPLE 4 --------------; DATA AMPERS; INPUT NAME & $25. AGE GENDER : $1.; DATALINES; RASPUTIN 45 M BETSY ROSS 62 F ROBERT LOUIS STEVENSON 75 M ; PROC PRINT; TITLE 'Example 4'; RUN; *-----------------------------------------; *---------------------- EXAMPLE 5.1 ----------------------; DATA COLINPUT; INPUT ID 1 HEIGHT 2-3 WEIGHT 4-6 GENDER $ 7 AGE 8-9; DATALINES; 168144M23 278202M34 362 99F37 461101F45 ; PROC PRINT; TITLE 'Example 5.1'; RUN; *----------------------------------------------------------;

*--------------------- EXAMPLE 5.2 -----------------------; DATA COLINPUT; INPUT ID 1 HEIGHT 2-3 WEIGHT 4-6 GENDER $ 7 AGE 8-9; DATALINES; 168144M23 278202M34 362 99F37 461101F45 ; PROC PRINT; TITLE 'Example 5.2'; RUN; *----------------------------------------------------------; *--------------- EXAMPLE 5.3 -------------; DATA COLINPUT; INPUT ID 1 AGE 8-9; DATALINES; 168144M23 278202M34 362 99F37 461101F45 ; PROC PRINT; TITLE 'Example 5.3'; RUN; *------------------------------------------; *--------------- EXAMPLE 5.4 -------------; DATA COLINPUT; INPUT AGE 8-9 ID 1 WEIGHT 4-6 HEIGHT 2-3 GENDER $ 7; DATALINES; 168144M23 278202M34 362 99F37 461101F45 ; PROC PRINT;

TITLE 'Example 5.4'; RUN; *------------------------------------------; *------------ EXAMPLE 6.1 ---------; DATA POINTER; INPUT @1 ID 3. @5 GENDER $1. @7 AGE 2. @10 HEIGHT 2. @13 DOB MMDDYY6.; FORMAT DOB MMDDYY8.; DATALINES; 101 M 26 68 012366 102 M 32 78 031460 103 F 45 62 112647 104 F 22 66 080170 ; PROC PRINT; TITLE 'Example 6.1'; RUN; *-----------------------------------; *------------- EXAMPLE 7.1 -------------; DATA POINTER; INPUT #1 @1 ID 3. @5 GENDER $1. @7 AGE 2. @10 HEIGHT 2. @13 DOB MMDDYY6. #2 @5 SBP 3. @9 DBP 3. @13 HR 3.; FORMAT DOB MMDDYY8.; DATALINES; 101 M 26 68 012366 101 120 80 68 102 M 32 78 031460 102 162 92 74 103 F 45 62 112647 103 134 86 74 104 F 22 66 080170 104 116 72 67 ; PROC PRINT; TITLE 'Example 7.1'; RUN; *----------------------------------------;

*------------- EXAMPLE 7.2 ---------; DATA SKIPSOME; INPUT #2 @1 ID 3. @12 SEX $6. #4; DATALINES; 101 256 RED 9870980 101 898245 FEMALE 7987644 101 BIG 9887987 101 CAT 397 BOAT 68 102 809 BLUE 7918787 102 732866 MALE 6856976 102 SMALL 3884987 102 DOG 111 CAR 14 ; PROC PRINT; TITLE 'Example 7.2'; RUN; *------------------------------------; *----------------- EXAMPLE 8.1 -----------------; DATA PARTS; INPUT @1 PARTID $14. @1 ST $2. @6 WT 3. @13 YR 2. @16 PARTDESC $24. @41 QUANT 4.; DATALINES; NY101110060172 LEFT-HANDED WHIZZER 233 MA102085112885 FULL-NOSE BLINK TRAP 1423 CA112216111291 DOUBLE TONE SAND BIT 45 NC222845071970 REVERSE SPIRAL RIPSHANK 876 ; PROC PRINT; TITLE 'Example 8.1'; RUN; *------------------------------------------------; *------------- EXAMPLE 9.1 ----------; DATA LONGWAY; INPUT ID 1-3 Q1 4 Q2 5 Q3 6 Q4 7 Q5 8

Q6 9-10 Q7 11-12 Q8 13-14 Q9 15-16 Q10 17-18 HEIGHT 19-20 AGE 21-22; DATALINES; 1011132410161415156823 1021433212121413167221 1032334214141212106628 1041553216161314126622 ; PROC PRINT; TITLE 'Example 9.1'; RUN; *-------------------------------------; *------------- EXAMPLE 9.2 ----------; DATA SHORTWAY; INPUT ID 1-3 @4 (Q1-Q5)(1.) @9 (Q6-Q10 HEIGHT AGE)(2.); DATALINES; 1011132410161415156823 1021433212121413167221 1032334214141212106628 1041553216161314126622 ; PROC PRINT; TITLE 'Example 9.2'; RUN; *-------------------------------------; *------------- EXAMPLE 9.3 -----------; DATA PAIRS; INPUT @1 ID 3. @6 (QN1-QN5)(1. +3) @7 (QC1-QC5)($1. +3) @26 (HEIGHT AGE)(2. +1 2.); DATALINES; 101 1A 3A 4B 4A 6A 68 26 102 1A 3B 2B 2A 2B 78 32 103 2B 3D 2C 4C 4B 62 45 104 1C 5C 2D 6A 6A 66 22 ; PROC PRINT;

TITLE 'Example 9.3'; RUN; *--------------------------------------; *------------- EXAMPLE 10 ------------; /*Single trailing:the trailing single @ tells the program not to go to the next data line for the next INPUT statement in the data step*/ /*or*/ /*To hold line pointer*/ DATA TRAILING; INPUT @6 TYPE $1. @ ; IF TYPE = '1' THEN INPUT AGE 1-2; ELSE IF TYPE = '2' THEN INPUT AGE 3-4; DROP TYPE; DATALINES; 23 1 44 2 ; PROC PRINT DATA=TRAILING; RUN; DATA MIXED; INPUT @20 TYPE $1. @; IF TYPE = '1' THEN INPUT ID 1-3 AGE 4-5 WEIGHT 6-8; ELSE IF TYPE = '2' THEN INPUT ID 1-3 AGE 10-11 WEIGHT 15-17; DATALINES; 00134168 1 00245155 1 003 23 220 2 00467180 1 005 35 190 2 ; PROC PRINT; RUN; data med; input pid visit drug $ @;output; input visit drug $ @;output; input visit drug $ @;output; input visit drug $ @;output;

cards; 100 1 5mg 2 5mg 3 10mg 4 15mg 101 1 10mg 2 10mg 3 15mg 4 15mg run; proc print;run; *--------------------------------------; *----------- EXAMPLE 11.1 --------; DATA LONGWAY; INPUT X Y; DATALINES; 12 34 56 69 10 12 13 14 ; PROC PRINT; TITLE 'Example 11.1'; RUN; *----------------------------------; *----------------------- EXAMPLE 11.2 --------------------; /*double trailing: line contains*/ /*more than one observation.*/ DATA SHORTWAY; INPUT X Y @@; DATALINES; 123456 6 9 10 12 13 14 ; PROC PRINT; RUN; Single trailing: Use a trailing @ at the end of the INPUT statement to hold the record in the input buffer for the execution of the next INPUT statement. Use an IF statement on the portion that is read in to test for a condition. If the condition is met, use another INPUT statement to read the remainder of the record to create an observation. If the condition is not met, the record is released and control passes back to the top of the DATA step.

1. Use a trailing @ at the end of the INPUT statement to hold the record in the input buffer for the execution of the next INPUT statement. 2. Use an IF statement on the portion that is read in to test for a condition. 3. If the condition is met, use another INPUT statement to read the remainder of the record to create an observation. 4. If the condition is not met, the record is released and control passes back to the top of the DATA step. 5. Reading the parts of data more than once. data red_team; input Team $ 13-18 @; if Team='red'; input IdNumber 1-4 StartWeight 20-22 EndWeight 24-26; datalines; 1023 David red 189 165 1049 Amelia yellow 145 124 1219 Alan red 210 192 1246 Ravi yellow 194 177 1078 Ashley red 127 118 1221 Jim yellow 220 . ; proc print data=red_team; run; data values informat format 30/02/2003 ddmmyy10. or s10. 30-02-2003 ddmmyy10. ddmmyyd10. 30:02:2003 ddmmyyc10. 30.02.2003 ddmmyyp10. 30 02 2003 ddmmyyb10. Eg:2 data values informat format 30/02/03 ddmmyy8. or s8. 30-02-03 ddmmyy8. ddmmyyd8. 30:02:03 ddmmyyc8. 30.02.03 ddmmyyp8. 30 02 03 ddmmyyb8.

Eg: data value informat format 02/30/2003 mmddyy10. ddmmyy10. invalid formats ddyymm10. yyddmm10. mm/yy/dd yy/mm/dd date value informat format eg: 23oct2003 date9. date9. 23oct03 date7. date7. dec2003 monyy7. monyy7. dec03 monyy5. monyy5.

data x; input a:mmddyy10.; format a ddmmyy10.; cards; 01oct2010 03feb2011 04jan2000 run; proc print;run; julian date informat format 2003032 julian7. julian7. 2003==>year 032==>no of days completed in year data x; input a:julian7.; format a julian7.; cards; 2003032 2004058 2005098 run; proc print;run; old date format:

worddate18.(max) worddate15.(min) data x; input a:ddmmyy10.; format a worddate18.; cards; 01-02-2003 02-04-2010 05-08-2011 run; proc print;run; worddate18.(max) worddate15.(min) weekdate24.(min) weekdate30(max) data x; input a:ddmmyy10.; format a worddate18.; data x; input a:ddmmyy10.; format a weekdate30.; cards; 01-02-2003 02-04-2010 05-08-2011 run; proc print;run; data x; input eid sal comma6. pf dollar11.; format sal comma6. pf dollar11.; cards; 100 23,000 $1345,000 101 34,000 $1,234,678 run; proc print;run; time datavalues informat format 10:12:23AM time10. timeampm12 02:23:52PM 22:10:20 10:12:20 time8. time8.

12oct2003:02:23:30pm 12oct2003:15:23:30 datetime20. dateampm22 datetime18. datetime18. data x; input id stime :time10. etime :time8.; format stime timeampm12. etime time8.; cards; 200 10:23:34am 15:34:19 201 02:23:34pm 18:23:34 run; proc print;run; 27th August 2011-08-27

Proc sort
Used for sorting analysis. By default it gives ascending order.if want to descending we need to specify descending option in sort procedure. /*duplicate data value*/ The same data value repeated in that variable. So it s called DDV. Duplicate data value can find based on required variable. /*duplicate observation*/ The same observation repeated in that data. duplicate observation can find out based on all variables in data. /*nodupkeys and noduprecs same*/ data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45

44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc sort data=x out=x1 noduprecs; by id; run; data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45

78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc sort data=x out=x1 noduprecs; by id sal age ; run; /*Double*/ we can give gap between observations. data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78

45 6785 78 45 6785 78 45 1000 90 ; run; proc print data=x double; run; /*noobs*/ using noobs option ,we can remove obs column from output. data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc print data=x noobs;

run; /*heading:*/ Using this option,we can change the column heading format (horizontal/vertical) for reporting. data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc print data=x heading=vertical; run; /*Width*/ using this option,we can give gap between the column(width=minimum/full).

data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc print data=x width=full; run; /*var*/ Using this statement,we can report required variable in specific order. data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24

100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc print data=x; var sal id; run; /*sum*/ Using this statement,we can do column wise sum for reporting. data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 77 5000 89 88 4999 45 44 8999 80 ; run; proc print data=x; sum sal;

run; /*id*/ Using this statement,we can replace the obs column. data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc print data=x; id id; sum sal; run; /*label statement*/ Using this statement,we change the variable names for reporting. If we write label statement in procedure block,these labels are temporary labels. data set block-permanent.

data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc print data=x label; label id='Empid' sal='Empsal'; run; /*Permanent labels*/ If we write in datasets, it will be permanent label. data x; input id sal age; label id='Empid' sal='Empsal' age='Empage';

cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 ; run; proc print data=x label;run; data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78

45 6785 78 45 6785 78 45 6785 78 45 1000 90 ; run; proc sort data=x out=x1; by id; run; proc print data=x1; by id; /*id id;*/ sum sal; run; data x; input id sal age; cards; 100 2000 24 100 7000 26 100 2000 24 100 2000 24 100 3000 25 101 4000 56 101 4000 56 101 4000 56 99 5999 67 88 8999 90 100 7000 26 99 7890 57 88 4000 28 77 5000 89 88 4999 45 44 8999 80 44 7000 68 44 8908 56 56 7890 89 56 8908 45 56 8908 45 56 8908 45 56 8908 45 78 4389 65 45 5678 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 6785 78 45 1000 90

; run; proc sort data=x out=x1; by id; run; proc print data=x1; by id; id id; sum sal; run; proc sort data=bank out=x1; by id; run; data tr(keep=id ct); set x1; by id; if first.id then ct=0; ct+1; if last.id; run; proc print;run; /*we can hold values across obseravation*/ or /*we can do cumulative totals.*/ data x(keep=a ct); input a b; /*retain ct 0;*/ /*ct=ct+a;*/ ct+a; cards; 10 20 20 80 30 90 40 80 . 10 20 10 run; proc print data=x; run; data x; input a b; k=sum(a,b); k1=a+b;

cards; 10 20 20 . 30 90 run; proc print data=x; run; data x; input id sal; cards; 10 2000 11 3000 12 4000 run; data x1; input id sal; cards; 14 8000 15 9000 11 8000 run; data c; set x x1; run; data c1; set c; if sal >3000; run; proc print;run; proc sort data=bank out=x1; by id; run; data tr(keep=id ct); set x1; by id; if first.id then ct=0; ct+1; if last.id; run; proc print;run; DATA MANAGEMENT Where: data company;

input company$ @8 area $ 9. invest strengh; cards; satyam vizag 2000000 567 tcs hyderabad 5000000 956 cts hyderabad 6000000 345 tcs madras 5000000 678 wipro hyderabad 7000000 789 wipro bangalore 6700000 456 satyam bangalore 6700000 453 wipro pune 6700000 789 hsbc hyderabad 8900000 673 icici bangalore 8000000 893 hdfc pune 9000000 796 run; proc print data=company; run; data satyam; set company; where company in('hdfc','tcs'); run; proc print;run; proc print data=company; where company='satyam' and invest ge 6000000; run; proc print data=company; where company eq 'satyam' or company='tcs';run; proc print data=company; where invest between 6000000 and 8000000;run; proc print data=company; where company ne 'satyam' or company ne'tcs';run; data x; input id sal; cards; 100 3000 101 4000 103 5000 102 6000 108 9000 105 6000 run; data x1; input id loc $; cards; 100 hyd 104 sec 110 ban 111 mum 108 hyd

102 ban run; proc sort data=x; by id;run; proc sort data=x1; by id;run; data lm; merge x(in=a) x1(in=b); by id; if a or b; run; proc print;run;

/*Left join*/ data lm; merge x(in=a) x1(in=b); by id; if a ; run; proc print;run; /*Right join*/ data rm; merge x(in=a) x1(in=b); by id; if b; run; proc print;run; /*inner join*/ data fm; merge x(in=a) x1(in=b); by id; if a and b; run; proc print;run; /*Full join*/ data lm; merge x(in=a) x1(in=b); by id; if a or b; run; proc print;run; data management:

is one type of data reading concept.using this concept to accept data from different sas files and arrange the data into specific order. data management concept runs on 2 concepts: 1.Adding 2.combine Adding: concept can be done in 2ways 1.appending 2.Concatination Appending:- To add one or more data set data into a existed data set. this concept is called appending. Append procedure:Using this procedure we can do appending and concatination. data x; input pid age gender $; cards; 100 34 female 101 56 male run; data x1; input pid age gender $; cards; 200 56 male 201 34 female run; data x2; input pid age gender $; cards; 300 58 male 301 39 female run; data x3; set x x1 x2; run; proc print;run; /*one to one merge*/ data demo; input id age sex $ weight; cards; 100 45 female 67 101 34 male 34 102 23 female 45 103 34 male 67 run; data medi; input id drug $ sdate:date9.;

cards; 102 col5mg 12oct2003 100 col10mg 15nov2003 101 col15mg 14dec2003 run; proc sort data=demo; by id;run; proc sort data=medi; by id;run; data onetoone; merge demo medi; by id; run; proc print;run; /*one to many*/ data demo; input id age sex $ weight; cards; 100 45 female 67 101 34 male 34 102 23 female 45 103 34 male 67 run; data lab; input id test $ units; cards; 100 hr 79 101 hr 78 102 hr 75 103 hr 76 100 sbp 178 102 sbp 178 103 sbp 145 100 dbp 89 101 dbp 88 102 dbp 85 103 dbp 89 run; proc sort data=demo; by id;run; proc sort data=lab; by id;run; data onemany; merge demo lab; by id; run; proc print;run; /*many to one*/

data demo; input id age sex $ weight; cards; 100 45 female 67 101 34 male 34 102 23 female 45 103 34 male 67 run; data lab; input id test $ units; cards; 100 hr 79 101 hr 78 102 hr 75 103 hr 76 100 sbp 178 102 sbp 178 103 sbp 145 100 dbp 89 101 dbp 88 102 dbp 85 103 dbp 89 run; proc sort data=demo; by id;run; proc sort data=lab; by id;run; data onemany; merge lab demo; by id; run; proc print;run; /*To run the merge concept based on matching*/ /*and non-matching observation.*/ data medi; input stno drug$ sdate edate; informat sdate edate date9.; format sdate edate date9.; cards; 210 col5mg 12jan2003 19jan2003 190 col10mg 14jan2003 20jan2003 203 col5mg 15jan2003 20jan2003 211 col10mg 15feb2003 18feb2003 178 col5mg 14mar2003 21mar2003 run; data exadvent; input stno exad$ sdate:date9.;

format sdate date9.; cards; 203 eardis 19jan2003 211 eyedis 16feb2003 run; proc sort data=medi;by stno;run; proc sort data=exadvent;by stno;run; /*To report who got expected adverse events and their medicine information.*/ data exadmed; merge exadvent(in=var) medi; by stno; if var=1; run; proc print data=exadmed;run; proc append: data demo1; input pid age sex $; cards; 100 34 female 101 56 male run; data demo2; input pid age sex $; cards; 200 56 male 201 34 female run; proc append base=demo1 data=demo2;run; data demo1; input pid age sex $ loc $; cards; 100 34 female hyd 101 56 male sec run; data demo2; input pid age sex $ grade; cards; 200 56 male 89 201 34 female 90 run; Force : In appending time ,additional variables are occured in transition daaset we can't run the append.If we want to run we should use force option.

proc append base=demo1 data=demo2 force;run; proc print;run; Update : Update :can be replace master data values with trasition data values based on matching variable. data oldemp1; input eid sal; cards; 100 4000 102 2000 103 3000 run; data newemp2; input eid sal; cards; 103 6000 100 7000 run; proc sort data=oldemp1; by eid;run; proc sort data=newemp2; by eid;run; data emp3; update oldemp1 newemp2; by eid; run; proc print;run; Both datasets have non-matching observations,in this cases if we run the update ,internally appending is running. data oldemp1; input eid sal; cards; 100 4000 102 2000 103 3000 104 9000 run; data newemp2; input eid sal; cards; 103 6000 100 7000 104 .

110 8000 120 9000 run; proc sort data=oldemp1; by eid;run; proc sort data=newemp2; by eid;run; data emp3; update oldemp1 newemp2; by eid; run; proc print;run; can be replace master data values with trasition data values based on matching variable. data oldemp1; input eid sal; cards; 100 4000 102 2000 103 3000 run; data newemp2; input eid sal; cards; 103 6000 100 7000 run; proc sort data=oldemp1; by eid;run; proc sort data=newemp2; by eid;run; data emp3; update oldemp1 newemp2; by eid; run; proc print;run; 27/08/11 data x2; set x; select; when (age <=10) group = "child "; when (11 <= age <= 19) group = "teenager "; when (20 <= age <= 29) group = "young adult"; when (30 <= age <= 45) group = "adult ";

when (46 <= age <= 59) otherwise group = "senior end; run; proc print;run;

group = "middle age "; ";

data x2; set x; select; when (age <=10) group = "child "; when (11 <= age <= 19) group = "teenager "; when (20 <= age <= 29) group = "young adult"; when (30 <= age <= 45) group = "adult "; when (46 <= age <= 59) group = "middle age "; otherwise group = "senior "; end; run; proc print;run; data x; input age sal; cards; 1 2000 2 3000 3 7000 4 8000 run; data x1; set x; select (Age); when (1) Limit = 110; when (2) Limit = 120; when (3) Limit = 130; otherwise limit=200; end; run; proc print;run; data x; input age sal; cards; 1 2000 2 3000 3 7000 4 8000

run; data x1; set x; select (Age); when (1) Limit = 110; when (2) Limit = 120; when (3) Limit = 130; otherwise limit=200; end; run; data x2; set x1; if limit=110 or limit=120 then group='senior'; else group='junior'; run; proc print;run; data x3; set x1; if limit in(110,120) then group='senior'; else group='junior'; run; proc print;run; data x; input age sal; cards; 1 2000 2 3000 3 7000 4 8000 run; data x4; set x; where age=4 ; run; proc print;run; data x; input age sal; cards; 1 2000 2 3000 3 7000 4 8000 run; data x4(where=( age=2)); set x; /*where age=4 ;*/

run; proc print;run; proc print data=x(where=( age=3));run; proc print data=x; where age=1; ;run; Do : data x; input age sal; cards; 1 2000 2 3000 3 7000 4 8000 5 8000 9 2000 10 5000 11 8000 12 9000 ; run; data x2; set x; if age in(1,4,5) then do; bonus=sal*0.2; hike=sal+bonus; end; else do; bonus=sal*0.5; hike=sal+bonus; end; run; proc print;run; loop concept: can be used to run required statements mutiple times.It must have 3 statements. 1.loop variable and some value 2.condition 3.increment output: can be used to store current observation in current data set.

data x; do i=1 to 10; end; run; proc print;run; data x1; do i=1 to 10; output; end; run; proc print;run; data x2; do i=1 to 10; k=i+1; output; end; run; proc print;run; data x3; do i=1 to 10; output; k=i+1; end; run; proc print;run; data x4; do i=1 to 10; output; i=i+1; end; run; proc print;run; data x5; do i=1 to 10; i=i+1; output; end; run; proc print;run; data one; x=10;output; x=5;output; run; data one two; x=10;output one; x=5;output two;

run; proc print ;run; data demo; i=100; do while(i<=120); cid=i; output; i=i+1; end; drop i; run; proc print;run; data compound; amount=5000; rate=.075; yearly=amount*rate; qty+((qty+amount)*rate/4);output; qty+((qty+amount)*rate/4);output; qty+((qty+amount)*rate/4);output; qty+((qty+amount)*rate/4);output; run;proc print;run; data comp; amount=5000; rate=.075; yearly=amount*rate; do qtr=1 to 4; quarterly+(quarterly+amount)*rate/4; end; run; proc print;run; data invest; do year=2001 to 2003; capital+5000; capital+(capital*.075); output; end; run; proc print;run; data xxx; do month='jan','feb','mar'; output; end;run;proc print;run; data xxx; do month=1,2,3,4,5,6; output;

end;run;proc print;run; data x; do i=1 to 20 by 2; output; end; run; proc print;run; data x; do i=14 to 2 by -2; output; end; run; proc print;run; data inv; do until(capital>1000000); year+1; capital+5000; capital+(capital* .075); output; end;run;proc print;run; data inv1; do while(capital<=1000000); year+1; capital+5000; capital+(capital* .075); output; end;run;proc print;run; 30/08/11 functions: Requires some arguments(variables or data values)and do some action and generate some result. These result will be stored in other variable. These are 3 types. 1.Numeric 2.character 3.date and time int:we can get integral part of variable. round:we can round up of the nearest integer or nearest decimal places. Absolute: This convert all data values into postive format. Mod: It returns remainder.

data x; input id age weight; cards; 100 23 78.76 101 24 56.45 102 34 -67.33 104 35 78.55 106 23 56.44 106 23 67.74 run; data x1; set x; wint=int(weight); wround=round(weight); wabs=abs(weight); run; proc print;run; data one; x=mod(20,5); run; proc print;run; data x; set sashelp.class; i=mod(_n_,2); if i=0; run; proc print;run; data x; set sashelp.class; i=mod(_n_,2); if i=1; run; proc print;run; SUM(): can be used to do row wise sum analysis or row wise sum. data x; input a b; x=sum(a,b); y=a+b; cards; 12 34 56 78 45 67 89 90 23 . run; proc print;run;

data x1; input fname $23. lname $; x=compress(fname,'j'); xgap=compress(fname); cards; ramudfghj jkkllliutreww k prasaddfg hhjjkloiyy b sasideqazx cvbnmkiu r run; proc print;run; string functions: 1.length(); using this function we can find out length of string. (no of character includes blankspaces). length function returns numeric value. index():It returns position of characters in string. it works based on characters and wordwise. Scan():using scan function we can get required word from string. or we can get nth word of string. Substr():Using this function,we can get part of string. It requires 3 arguments. 1.variable name 2.starting postion 3.number of characters. data x; input name $13. id; x=length(name); xi=index(name,'k'); xw=index(name,'an'); xs=scan(name,2); xst=substr(name,1,4); xst1=substr(name,3); cards; shahrukh khan 100 salman khan 101 ameer khan 102 prasad babu 103 ; run; proc print;run; concatination(combine):It is used for combining

the strings.symbol is ||. data x1; length k $24.; input fname $23. lname $; k=fname||lname; cards; ramudfghjjkkllliutreww k prasaddfghhjjkloiyy b sasideqazxcvbnmkiu r run; proc print;run; compress():can be used to remove specific character from the string. it requires 2 arguments. it is working based on character wise. note:if we ommit 2nd argument in compress function,it removes the blank spaces from the string. data x1; input fname $23. lname $; x=compress(fname,'j'); xgap=compress(fname); cards; ramudfghj jkkllliutreww k prasaddfg hhjjkloiyy b sasideqazx cvbnmkiu r run; proc print;run; Translate():can be used to replace the required character in string. data x2; input name $ sal; x=translate(name,'a','x'); cards; prasad 2000 ramesh 3000 rajesh 4000 run; proc print;run; propcase(): to captialize first letter of string. data x2; input name $ sal; xp=propcase(name);

cards; prasad 2000 ramesh 3000 rajesh 4000 run; proc print;run; semantic errors: if we send wrong number of arguments for functions.In this case we will get one type of execution error.this execution error is called semantic error.

31/08/11 cat(): concatenates strings.it without removes leading and trailing blanks. cats():concatenates strings.it removes leading and trailing blanks. catt():concatenates strings.it removes only trailing blanks. catx():concatenates strings.it removes leading and trailing blanks and insert separators. data x; x=' abc'; x1=' abc '; y=' 123 '; k=cat(x1,y); ks=cats(x,y); kt=catt(x1,y); kx=catx(',',x1,y); run; proc print;run; cat():concatenates strings.it without removes leading and trailing blanks. cats():concatenates strings.it removes leading and trailing blanks. catt():concatenates strings.it removes

only trailing blanks. catx():concatenates strings.it removes leading and trailing blanks and insert separators. data x; x=' abc'; x1=' abc '; y=' 123 '; k=cat(x1,y); ks=cats(x,y); kt=catt(x1,y); kx=catx(',',x1,y); run; proc print;run; tranwrd(): It removes or replace all occurances of a word in string. data x1; name='prasad babu mr'; x=tranwrd(name,'mr','mrs'); run; proc print;run; quote(): It add double quotation marks to string. data x2; input name $; x=quote(name); cards; abc xyz ; run; proc print;run; dquote(): It removes quotation marks. data x2; input name $; x=quote(name); y=dquote(x); cards; abc xyz ; run; proc print;run; date/timeformat data demo;

input id svdate:date9. svtime:time8.; format svdate date9. svtime time8.; cards; 100 12jan2010 12:23:34 101 13feb2003 13:23:34 102 14feb2009 11:23:34 103 15mar2011 10:23:23 run; data x; set demo; svday=day(svdate); svmonth=month(svdate); svyear=year(svdate); svhour=hour(svtime); scmin=minute(svtime); svsec=second(svtime); run; proc print;run; data x; input id svdtime:datetime18.; format svdtime datetime18.; cards; 100 12jan2003:12:23:24 101 13feb2005:13:23:34 run; data x1; format xdate date9. xtime time8.; set x; xdate=datepart(svdtime); xtime=timepart(svdtime); run; proc print;run; data demo; input id svdate:date9. svtime:time8.; format svdate date9. svtime time8.; cards; 100 12jan2010 12:23:34 101 13feb2003 13:23:34 102 14feb2009 11:23:34 103 15mar2011 10:23:23 run; data x; set demo; svday=day(svdate); svmonth=month(svdate); svyear=year(svdate); svhour=hour(svtime); scmin=minute(svtime);

svsec=second(svtime); run; proc print;run; datepart(): can be used to get datevalue from the date and time variable. timepart(): can be used to get timevalue from the date and time variable. data x; input id svdtime:datetime18.; format svdtime datetime18.; cards; 100 12jan2003:12:23:24 101 13feb2005:13:23:34 run; data x1; format xdate date9. xtime time8.; set x; xdate=datepart(svdtime); xtime=timepart(svdtime); run; proc print;run; intck():can be used to report difference between the datevalues in day interval,month interval or year interval. data x; input id (sdate edate) (:date9.); format sdate edate date9.; days=intck('day',sdate,edate); months=intck('month',sdate,edate); year=intck('year',sdate,edate); cards; 100 12jan2003 14dec2003 101 15jan2003 14oct2004 run; proc print;run; INTNX(): can be used to increments dates by intervals data x; input id (sdate edate) (:date9.); format sdate edate newdate newmonth newyear date9. ; newdate=intnx('day',sdate,10); newmonth=intnx('month',sdate,2); newyear=intnx('year',sdate,2); cards; 100 12jan2003 14dec2003

101 15jan2003 14oct2004 run; proc print;run; INTNX has three required arguments and one optional argument, commonly used as follows for SAS date values. INTNX(interval, start-from, increment <,alignment>); interval is the unit of measure (days, weeks, months, quarters, years, etc.) by which start-from is incremented. start-from is a SAS date value to be incremented. increment is the integer number of intervals by which start-from is incremented (negative values = earlier dates). alignment is where start-from is aligned within interval prior to being incremented. Possible values are beginning, middle, end, and (new in Version 9) sameday. The default value is beginning. data x; input id (sdate edate) (:date9.); format sdate edate newdate newmonth newyear date9. ; newdate=intnx('day',sdate,10,'B'); newmonth=intnx('month',sdate,2); newyear=intnx('year',sdate,2); cards; 100 12jan2003 14dec2003 101 15jan2003 14oct2004 run; proc print;run; data demo; input id svdate:date9. svtime:time8.; format svdate date9. svtime time8.; cards; 100 12jan2010 12:23:34 101 13feb2003 13:23:34 102 14feb2009 11:23:34 103 15mar2011 10:23:23 run; data x; set demo; svday=day(svdate); svmonth=month(svdate); svyear=year(svdate); svhour=hour(svtime);

scmin=minute(svtime); svsec=second(svtime); run; proc print;run; datepart(): can be used to get datevalue from the date and time variable. timepart(): can be used to get timevalue from the date and time variable. data x; input id svdtime:datetime18.; format svdtime datetime18.; cards; 100 12jan2003:12:23:24 101 13feb2005:13:23:34 run; data x1; format xdate date9. xtime time8.; set x; xdate=datepart(svdtime); xtime=timepart(svdtime); run; proc print;run; intck():can be used to report difference between the datevalues in day interval,month interval or year interval. data x; input id (sdate edate) (:date9.); format sdate edate date9.; days=intck('day',sdate,edate); months=intck('month',sdate,edate); year=intck('year',sdate,edate); cards; 100 12jan2003 14dec2003 101 15jan2003 14oct2004 run; proc print;run; INTNX(): can be used to increments dates by intervals data x; input id (sdate edate) (:date9.); format sdate edate newdate newmonth newyear date9. ; newdate=intnx('day',sdate,10); newmonth=intnx('month',sdate,2); newyear=intnx('year',sdate,2); cards;

100 12jan2003 14dec2003 101 15jan2003 14oct2004 run; proc print;run; INTNX has three required arguments and one optional argument, commonly used as follows for SAS date values. INTNX(interval, start-from, increment <,alignment>); interval is the unit of measure (days, weeks, months, quarters, years, etc.) by which start-from is incremented. start-from is a SAS date value to be incremented. increment is the integer number of intervals by which start-from is incremented (negative values = earlier dates). alignment is where start-from is aligned within interval prior to being incremented. Possible values are beginning, middle, end, and (new in Version 9) sameday. The default value is beginning. data x; input id (sdate edate) (:date9.); format sdate edate newdate newmonth newyear date9. ; newdate=intnx('day',sdate,10,'B'); newmonth=intnx('month',sdate,2); newyear=intnx('year',sdate,2); cards; 100 12jan2003 14dec2003 101 15jan2003 14oct2004 run; proc print;run; data x; input id (sdate edate) (:date9.); format sdate edate newdate newmonth newyear date9. ; newdate=intnx('day',sdate,-10); newmonth=intnx('month',sdate,-2); newyear=intnx('year',sdate,-2); cards; 100 12jan2003 14dec2003 101 15jan2003 14oct2004 run; proc print;run; Procedures:

Proc Means proc means: can be used to generate summary statistical analysis. data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market sum mean n; run; data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run;

proc means data=market sum mean n; run; var: requires analysis variable.analysis variable must be numeric. data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market; var sale; run; data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09

101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market; var sale stock; run; class: requires grouping variable.This variable is also called as classification variable. class variable take either character or numeric. data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market; class area; var sale stock; run; proc means: can be used to generate summary statistical analysis. data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18

101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market sum mean n; run; data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market sum mean n; run; var: requires analysis variable.analysis variable must be numeric. data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10

108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market; var sale; run; data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market; var sale stock; run; class: requires grouping variable.This variable is also called as classification variable. class variable take either character or numeric. data market; input pno area $ product $ stock sale; cards;

101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market; class area; var sale stock; run; data market; input pno area $ product $ stock sale; cards; 101 vizag lux 10 9 109 hyd lux 16 12 101 hyd cintol 78 76 110 hyd cintol 78 76 110 hyd cintol 12 10 108 vizag rin 15 10 108 vizag lux 23 18 101 vij lux 20 19 190 hyd rin 10 09 190 hyd lux 10 10 110 vizag lifebuoy 13 11 145 vizag lifebuoy 13 11 110 vizag cintol 19 17 101 vizag lux 13 12 108 vizag lifebuoy 10 11 190 hyd lifebuoy 10 09 101 vij lifebuoy 13 12 110 vizag lifebuoy 12 14 run; proc means data=market; class area; var sale stock; run;

/*sales wise analysis*/ proc means data=market; var sale;run; /*sales wise analysis based on product*/ proc means data=market; class product; var sale; run; /*sales wise analysis based on area*/ proc means data=market; class area; var sale; run; /*All types of analysis*/ printalltypes:can be used to generate all types of analysis based classification variables and analysis variables. proc means data=market printalltypes; class area product; var sale; run; 01/09/11 data one; input res$@@; cards; y n y n n n y y nnn run; proc means data=one; class res; run; proc freq data=one; table res; run; data medi3; input group$ week$ drug$ sub; cards; g100 week1 col5mg 90 g200 week1 col5mg 90 g300 week1 col5mg 90 g100 week2 col5mg 70 g200 week2 col10mg 80 g300 week3 col15mg 68 g200 week3 col15mg 64 g300 week4 col15mg 60 g200 week4 col15mg 60 g300 week4 col5mg 64

run; proc print data=medi3;run; proc freq data=medi3; table group drug week;run; proc freq data=medi3; table group /nopercent norow nocol;; weight sub; run; proc freq data=medi3; table group*drug/nopercent norow nocol; run; proc freq data=medi3; table group*drug/nopercent norow nocol; weight sub; run; proc means data=medi3 sum; class group drug; var sub; run; proc freq data=medi3; table group*drug/nopercent norow nocol; weight sub; run; proc freq data=medi3; table group*week/nopercent norow nocol; weight sub; run; proc sort data=medi3; by group; run; proc freq darta=medi3; table week*drug/nopercent norow nocol; by group;run; proc freq data=medi3; table week*drug/nopercent norow nocol; by group; weight sub; run; data medi4; input group$ week$ drug$ sub; cards; g100 week1 col5mg 90 . week1 col5mg 90 g300 week1 col5mg 90 g100 week2 col5mg 70 g200 week2 col10mg 80 . week3 col15mg 68 g200 week3 col15mg 64 g300 week4 col15mg 60 g200 week4 col15mg 60

g300 week4 col5mg 64 run; proc print data=medi4;run; proc freq data=medi4; table group/missing out=medi5;run; proc format; value $gp ' '='miss'; run; proc freq data=medi4; table group/missing out=medi6; format group $gp.; run; data hdr; input pid (dos1-dos3)($); cards; 100 y n y 101 y y y 102 y n n 103 y y n 104 n y y 105 n n y 106 y y n 107 y y y run;proc print data=hdr;run; /*Who has taken dos1*/ proc freq data=hdr; table dos1; where dos1='y'; run; proc freq data=hdr; table dos1; where dos1='y' and dos2='n' and dos3='n'; run; /*Who has taken dos1 and dos2*/ proc freq data=hdr; table dos1*dos2/nopercent norow nocol; where dos1='y' and dos2='y' and dos3='n'; run; 02/09/11 Proc format: can be used to create user defined informats and formats. Invalue statement: can be used to create user defined informats for data reading. proc format; invalue ds 'l'=0.05

'm'=0.1 'h'=0.15; run; data medi; input stno week$ drug$ dose:ds.; cards; 100 week1 col l 101 week1 col l 102 week1 col m 100 week2 col h 101 week2 col m 102 week2 col h run;proc print;run; Value statement: can be used to create user defined formats for reporting. proc format; value $wk 'w1'='week1' 'w2'='week2'; value dg 1='coll5mg' 2='col10mg' 3='col15mg'; run; data medi1; input stno week$ drug; format week $wk. drug dg.; cards; 100 w1 1 101 w1 2 100 w2 2 102 w2 3 101 w2 1 102 w2 3 run; proc print data=medi1;run; proc format; value gen 2='Female' 1='Male'; run; data x4; input name $ sal sex ; format sex gen.; cards; raju 2000 1

rani 3000 2 ramu 4000 1 run; proc print;run; proc format; value $wk 'w1'='week1' 'w2'='week2' 'w3'='week3'; value dg 1='coll5mg' 2='col10mg' 3='col15mg'; run; data x; input id week $ dose; format week $wk. dose dg.; cards; 100 W1 1 101 W1 2 100 W2 2 101 W2 1 100 W3 2 101 W3 3 run; proc print;run;

proc format; invalue sal low-1000=1000 1000<-<3000=3000 3000-5000=5000 5001<-high=_same_; run; data emp; input eid salary:sal.; cards; 100 500 234 700 345 1000 456 2300 345 1690 478 4000 567 5600 678 7900 908 9800 876 12000 run; proc print;run;

Picture statement: can be used to create templete. proc format; picture rpt low-<150='999 normal stage' 150-<180='999 control stage' 180-high='999 uncontrol stage'; run; data x; input id sbp; format sbp rpt.; cards; 100 89 101 145 102 190 103 160 104 155 105 4 run; proc print;run; /*INPUT(source, informat.) */ /*Character to numeric*/ data x; input id sal $; sal_num=input(sal,best.); cards; 100 20000 101 3000 103 40000 run; proc print;run; PUT(source, format.) data demo2; set medi; g1=put(gender,gn.); w1=input(week,$wk.); d1=input(drug,ds.); run; proc print data=demo2;run;

03/9/11

SQL:structured query language

Using SQL concept,we can handle any data base. Using SQL concept,we can generate result in sas environment. SQL concept mainly running based on four concepts. 1.DDL-data definition language 2.DML-data manuplication language 3.DCL-data controlling language 4.query language DDL-Using concept,we can create a table with variables without obseravations(null data set). DML:using this concept,we can insert data in existed table,update data values, delete the obseravations from existed table. DCL:We can control data process. Query language: Using with Query language we can retrieve the data for reporting and storage. create statement: can be used to create the tables with variables and without observation. insert statement: can be used to insert data in existed table. As insert statement is working based on value clause or set clause. Value clause is working based on variable postion. set clause is working based on variable name. Select statement: is a query statement.It can be used to retrieve data for reporting and storage. insert statement: can be used to insert data in existed table. proc sql; create table prasad(name char,age int,sal int); quit; proc sql;

insert into prasad values('prasad',20,20000); insert into prasad values('raju',30,80000); insert into prasad set name='mahesh',age=30,sal=40000; insert into prasad set age=35,sal=50000,name='shahrukh'; quit; eg: proc sql; create table prasad(name char(10),age int,sal int); quit; proc sql; insert into prasad values('prasad',20,20000) values('raju',30,80000) values('mahi',35,6000); insert into prasad set name='mahesh',age=30,sal=40000 set age=35,sal=50000,name='shahrukh' set age=40,sal=60000,name='salmankhan'; quit; proc sql; create table x(Name char,Sex char, Age int,Height int,Weight int); insert into x select * from sashelp.class; quit; proc sql; create table x1(Name char,Sex char, Age int,Height int,Weight int); insert into x select * from sashelp.class where sex="F"; quit; /*Reporting*/ proc sql; select * from sashelp.class where age>=15; quit; /*Storage*/ proc sql; create table x3 as select * from sashelp.class where age>=15; quit; data x; input age sal;

cards; 10 2000 21 3000 35 7000 47 8000 20 9000 16 1000 14 7000 19 9000 55 9000 65 9000 58 9000 43 7654 34 8765 22 8765 33 9086 43 9874 24 4563 48 2342 23 7896 run; data x2; set x; select; when (age <=10) group = "child "; when (11 <= age <= 19) group = "teenager "; when (20 <= age <= 29) group = "young adult"; when (30 <= age <= 45) group = "adult "; when (46 <= age <= 59) group = "middle age "; otherwise group = "senior "; end; run; proc sql; create table x5 as select *,case when (age <=10) then "child when (11 <= age <= 19) when (20 <= age <= 29) when (30 <= age <= 45) when (46 <= age <= 59) else "senior " end as group from x; quit; proc print;run; proc compare base=x2 compare=x5; var group;

" then "teenager " then "young adult" then "adult " then "middle age "

with group; run; Update statement: can be used to modify the data values in existed variable. Syntax: update <table name> set <variable name>=<expression> alter statement: can be used to modify the existed table. modifications: 1.Adding new variable 2.adding or droped required column. 3.assign constraints 4.delete constraints proc sql; alter table emp add anualsal num ; quit; proc sql; update emp set anualsal=salary*12; quit; proc sql; alter table emp add anualsal num, bonus num, netsal num; update emp set anualsal=salary*12; update emp set bonus=salary*0.5; update emp set netsal=anualsal+bonus; quit;

input pid sbp; cards; 100 156 101 176 102 140 103 180 104 145 105 167 run; proc sql;

create table case2 as select *, case when sbp>=170 then '15mg' when sbp>=150 and sbp<170 then '10mg' else '5mg' end as drug, case when sbp>=170 then 3 when sbp<170 then 2 else 3 end as dailydose from med;quit;

SQL:structured query language Using SQL concept,we can handle any data base. Using SQL concept,we can generate result in sas environment. SQL concept mainly running based on four concepts. 1.DDL-data definition language 2.DML-data manuplication language 3.DCL-data controlling language 4.query language DDL-Using concept,we can create a table with variables without obseravations(null data set). DML:using this concept,we can insert data in existed table,update data values, delete the obseravations from existed table. DCL:We can control data process. Query language: Using with Query language we can retrieve the data for reporting and storage. create statement: can be used to create the tables with variables and without observation. insert statement: can be used to insert data in existed table. As insert statement is working based on value clause or set clause.

Value clause is working based on variable postion. set clause is working based on variable name. Select statement: is a query statement.It can be used to retrieve data for reporting and storage. insert statement: can be used to insert data in existed table. proc sql; create table prasad(name char,age int,sal int); quit; proc sql; insert into prasad values('prasad',20,20000); insert into prasad values('raju',30,80000); insert into prasad set name='mahesh',age=30,sal=40000; insert into prasad set age=35,sal=50000,name='shahrukh'; quit; proc sql; create table prasad(name char(10),age int,sal int); quit; proc sql; insert into prasad values('prasad',20,20000) values('raju',30,80000) values('mahi',35,6000); insert into prasad set name='mahesh',age=30,sal=40000 set age=35,sal=50000,name='shahrukh' set age=40,sal=60000,name='salmankhan'; quit; proc sql; create table x(Name char,Sex char, Age int,Height int,Weight int); insert into x select * from sashelp.class; quit; proc sql; create table x1(Name char,Sex char, Age int,Height int,Weight int); insert into x select * from sashelp.class where sex="F"; quit;

proc sql; create table x2 as select * from sashelp.shoes; quit; Order by: can be used to report the data in ascending or descending order. Default ascending order. If we want to descending order we use descending or desc option in after variable name of order by clause. proc sql; create table x3 as select * from sashelp.class order by age; quit; proc sql; create table x3 as select * from sashelp.class order by age desc; quit; where clause: can be used to create a subset of data for reporting and storage. proc sql; create table x3 as select * from sashelp.class where age>=15; quit; /*Reporting*/ proc sql; select * from sashelp.class where age>=15; quit; /*Storage*/ proc sql; create table x3 as select * from sashelp.class where age>=15; quit; /*To store age>=14 sub and generate report in descending based on weight variable*/ proc sql;

create table x3 as select * from sashelp.class where age>=15 order by weight desc; quit; proc sql; create table x1 as select name,age from sashelp.class; quit; proc print;run; data emp; input eid salary sale; cards; 100 2000 500 101 3000 300 102 4000 600 104 4000 900 105 5000 400 106 6000 500 107 7000 400 108 8000 400 109 6000 300 ; run; data x4; set emp; if sale < 500 then nsal=2000; else if 501<sale<700 then nsal=4000; else nsal=5000; run; proc print;run; proc sql; create table x4 as select *,case when sale Lt 500 then 2000 when sale gt 501 and sale Lt 700 then 4000 else 5000 end as nsal from emp; quit; data x; input eid salary sale; cards; 100 2000 500 101 3000 300 102 4000 600 104 4000 900 105 5000 400 106 6000 500

107 7000 400 108 8000 400 109 6000 300 ; run; proc sql; create table x4 as select *,salary+case when sale ge 500 then 2000 else 100 end as nsal from x; quit; proc sql; create table x6 as select *,case when sale ge 500 then 2000 else 100 end as nsal from x; quit; proc sql; create table x7 as select *,salary+nsal as net from x6; quit; proc compare base=x4 compare=x7; var nsal; with net; run; data x; input age sal; cards; 10 2000 21 3000 35 7000 47 8000 20 9000 16 1000 14 7000 19 9000 55 9000 65 9000 58 9000 43 7654 34 8765 22 8765 33 9086 43 9874 24 4563 48 2342 23 7896

run; data x2; set x; select; when (age <=10) group = "child "; when (11 <= age <= 19) group = "teenager "; when (20 <= age <= 29) group = "young adult"; when (30 <= age <= 45) group = "adult "; when (46 <= age <= 59) group = "middle age "; otherwise group = "senior "; end; run; proc sql; create table x5 as select *,case when (age <=10) then "child " when (11 <= age <= 19) then "teenager " when (20 <= age <= 29) then "young adult" when (30 <= age <= 45) then "adult " when (46 <= age <= 59) then "middle age " else "senior " end as group from x; quit; proc print;run; proc compare base=x2 compare=x5; var group; with group; run; Update statement: can be used to modify the data values in existed variable. Syntax: update <table name> set <variable name>=<expression> alter statement: can be used to modify the existed table. modifications: 1.Adding new variable 2.adding or droped required column. 3.assign constraints 4.delete constraints proc sql; alter table emp add anualsal num ; quit; proc sql;

update emp set anualsal=salary*12; quit; proc sql; alter table emp add anualsal num, bonus num, netsal num; update emp set anualsal=salary*12; update emp set bonus=salary*0.5; update emp set netsal=anualsal+bonus; quit; data x; input eid salary sale; cards; 100 2000 500 101 3000 300 102 4000 600 104 4000 900 105 5000 400 106 6000 500 107 7000 400 108 8000 400 109 6000 300 ; run; proc sql; alter table x add nsal num; quit; proc sql; update x set nsal= salary+case when sale ge 500 then 2000 when sale ge 400 and sale lt 500 then 1500 else 100 end; quit; data med; input pid sbp; cards; 100 156 101 176 102 140 103 180 104 145 105 167 run; proc sql; create table case2 as select *, case

when sbp>=170 then '15mg' when sbp>=150 and sbp<170 then '10mg' else '5mg' end as drug, case when sbp>=170 then 3 when sbp<170 then 2 else 3 end as dailydose from med; quit; Operators: Using operators we can add the tables for reporting. Types: 1.union all 2.union 3.intersect 4.except 1. union all: Can be used to add the report in sequential order for reporting. Or produces all rows from both queries. OUTER UNION concatenates the query results. UNION produces all unique rows from both queries. data lab1; input stno test$ units; date='12jan2003'd; format date date9.; cards; 100 hr 78 101 hr 90 100 dbp 89 100 sbp 156 101 dbp 90 101 sbp 178 102 hr 78 run; proc print data=lab1; run; data lab2; input stno test$ units;

date='13jan2003'd; format date date9.; cards; 102 hr 78 103 hr 90 103 dbp 89 103 sbp 156 102 dbp 90 102 sbp 178 102 sbp 178 102 sbp 178 run; proc sql; create table unall as select * from lab1 union all select * from lab2; quit; data u; set lab1 lab2; run; proc print;run; proc sql; create table un as select * from lab1 union select * from lab2; quit; INTERSECT produces rows that are common to both query results. data ex; input stno ad $ date:date9.; format date date9.; cards; 100 eyedis 12jan2003 105 eardis 12jan2003 102 eyedis 12jan2003 run; data unex; input stno ad $ date:date9.; format date date8.; cards; 103 nervous 12jan2003 104 coma 12jan2003 105 eardis 12jan2003 run; proc sql;

select * from ex intersect select * from unex; quit; EXCEPT produces rows that are part of the first query only. proc sql; select * from unex except select * from ex;quit; data ex; input stno ad $ date:date9.; format date date9.; cards; 100 eyedis 12jan2003 105 eardis 12jan2003 102 eyedis 12jan2003 run; data unex; input stno ad $ date:date9.; format date date8.; cards; 103 nervous 12jan2003 104 coma 12jan2003 105 eardis 12jan2003 run; proc sql; select * from ex intersect select * from unex; quit; proc sql; select * from unex except select * from ex;quit; proc sql; (select * from ex except select * from unex) union (select * from unex except select * from ex); quit;

Joins: 1.simple join 2.inner join 3.outer join======1.left join or left outer join 2.right join or right outer join 3.full join or full outer join 4.natural join 5.self join simple join: we can report matching observations from the required data sets. data event; input stno exad$ exdate:date9.; format exdate date9.; cards; 230 eyedis 12feb2005 456 skinprb 15feb2005 345 cold 16mar2005 run; data unevent; input stno unexad$ uexdate:date9.; format uexdate date9.; cards; 230 nervous 28feb2005 156 eardis 17mar2005 245 diabets 18mar2005 run; proc sql; select * from event as ex, unevent as uex where ex.stno=uex.stno; quit; inner join: works like a simple join.But inner join can be used between two tables. on clause can be used instead of where clause. inner join can be activated with inner join. proc sql; create table innerjoin as select * from event as ex inner join unevent as uex on ex.stno=uex.stno;

quit; left join: reports all observation from left side table and only matching observations from condition based right side table. proc sql; create table leftjoin as select * from event as ex left join unevent as uex on ex.stno=uex.stno; quit; Right join: reports all observation from right side table and only matching observations from condition based left side table. proc sql; create table rightjoin as select * from event as ex right join unevent as uex on ex.stno=uex.stno; quit; Full join: Reports all observation from 2 tables and match the rows. proc sql; create table fulljoin as select * from event as ex full join unevent as uex on ex.stno=uex.stno; quit; Natural join: we can report matching observation from required data sets without using any condition.

proc sql; create table natural as select * from event natural join unevent; quit; self join: If we join the table internally with same table then it is called self join. data trt9; input stno bsbp drug$ asbp; cards; 190 167 col5mg 178 123 178 col15mg 167 198 167 col10mg 146 237 172 col10mg 134 run; proc sql; select *from trt9 where bsbp> asbp; quit; Cartsign product: proc sql; create table cartsign as select * from event as ex, unevent as uex ; quit; /*Equivalent SQL and datastep coding for joins*/ data event; input stno exad$ exdate:date9.; format exdate date9.; cards; 230 eyedis 12feb2005 456 skinprb 15feb2005 345 cold 16mar2005 run; data unevent; input stno unexad$ uexdate:date9.; format uexdate date9.; cards; 230 nervous 28feb2005 156 eardis 17mar2005 245 diabets 18mar2005 run; proc sql; create table simplejoin as

select * from event as ex, unevent as uex where ex.stno=uex.stno; quit; proc sql; create table innerjoin as select * from event as ex inner join unevent as uex on ex.stno=uex.stno; quit; proc sort data=event out=x; by stno;run; proc sort data=unevent out=x1; by stno;run; data inner; merge x(in=a) x1(in=b); by stno; if a and b; run; proc print;run; proc sql; create table leftjoin as select * from event as ex left join unevent as uex on ex.stno=uex.stno; quit; data left; merge x(in=a) x1(in=b); by stno; if a ; run; proc print;run; proc sql; create table rightjoin as select * from event as ex right join unevent as uex on ex.stno=uex.stno; quit; data right; merge x(in=a) x1(in=b); by stno; if b ; run; proc print;run;

proc sql; create table fulljoin as select * from event as ex full join unevent as uex on ex.stno=uex.stno; quit; data full; merge x(in=a) x1(in=b); by stno; if a or b ; run; proc print;run; Joins: 1.simple join 2.inner join 3.outer join======1.left join or left outer join 2.right join or right outer join 3.full join or full outer join 4.natural join 5.self join simple join: we can report matching observations from the required data sets. data event; input stno exad$ exdate:date9.; format exdate date9.; cards; 230 eyedis 12feb2005 456 skinprb 15feb2005 345 cold 16mar2005 run; data unevent; input stno unexad$ uexdate:date9.; format uexdate date9.; cards; 230 nervous 28feb2005 156 eardis 17mar2005 245 diabets 18mar2005 run; proc sql; select * from event as ex, unevent as uex where ex.stno=uex.stno;

quit; inner jion: works like a simple join.But inner join can be used between two tables. on clause can be used instead of where clause. inner join can be activated with inner join. proc sql; create table innerjoin as select * from event as ex inner join unevent as uex on ex.stno=uex.stno; quit; left join: reports all observation from left side table and only matching observations from condition based right side table. proc sql; create table leftjoin as select * from event as ex left join unevent as uex on ex.stno=uex.stno; quit; Right join: reports all observation from right side table and only matching observations from condition based left side table. proc sql; create table rightjoin as select * from event as ex right join unevent as uex on ex.stno=uex.stno; quit; Full join: Reports all observation from 2 tables and match the rows.

proc sql; create table fulljoin as select * from event as ex full join unevent as uex on ex.stno=uex.stno; quit; Natural join: we can report matching observation from required data sets without using any condition. proc sql; create table natural as select * from event natural join unevent; quit; self join: If we join the table internally with same table then it is called self join. data trt9; input stno bsbp drug$ asbp; cards; 190 167 col5mg 178 123 178 col15mg 167 198 167 col10mg 146 237 172 col10mg 134 run; proc sql; select *from trt9 where bsbp> asbp; quit; Cartsign product: proc sql; create table cartsign as select * from event as ex, unevent as uex ; quit; /*Equivalent SQL and datastep coding for joins*/ data event; input stno exad$ exdate:date9.;

format exdate date9.; cards; 230 eyedis 12feb2005 456 skinprb 15feb2005 345 cold 16mar2005 run; data unevent; input stno unexad$ uexdate:date9.; format uexdate date9.; cards; 230 nervous 28feb2005 156 eardis 17mar2005 245 diabets 18mar2005 run; proc sql; create table simplejoin as select * from event as ex, unevent as uex where ex.stno=uex.stno; quit; proc sql; create table innerjoin as select * from event as ex inner join unevent as uex on ex.stno=uex.stno; quit; proc sort data=event out=x; by stno;run; proc sort data=unevent out=x1; by stno;run; data inner; merge x(in=a) x1(in=b); by stno; if a and b; run; proc print;run; proc sql; create table leftjoin as select * from event as ex left join unevent as uex on ex.stno=uex.stno; quit; data left; merge x(in=a) x1(in=b); by stno; if a ;

run; proc print;run; proc sql; create table rightjoin as select * from event as ex right join unevent as uex on ex.stno=uex.stno; quit; data right; merge x(in=a) x1(in=b); by stno; if b ; run; proc print;run; proc sql; create table fulljoin as select * from event as ex full join unevent as uex on ex.stno=uex.stno; quit; data full; merge x(in=a) x1(in=b); by stno; if a or b ; run; proc print;run; aggregate functions: using aggregate function,we can do arthematic manipulation in sql.using aggregate function,we can do column and row wise analysis. data clinical9; input center$ trail$ sub adsub; cards; appolo phase1 67 12 nims phase1 75 14 nims phase1 80 40 care phase1 34 10 care phase2 40 20 appolo phase2 267 22 nims phase2 178 14 care phase2 234 30 appolo phase1 245 50 appolo phase2 260 50

run; proc sql; create table aggregate as select center,trail,sum(sub) as total from clinical9 group by center, trail; quit; data x; length key $14.; set clinical9; key=compress(center||trail); run; proc sort data=x out=x1; by key;run; data x2; set x1; by key; if first.key then ct=0; ct+sub; if last.key; run; proc print;run; proc sql; create table one as select trail,count(trail) as obs from clinical9 group by trail; quit; proc sort data=clinical9 out=y; by trail;run; data one1; set y; by trail; if first.trail then ct=0; ct+1; if last.trail; keep trail ct; run; proc print;run; data clinical; infile cards truncover; input center $ trail $ sub adsub; cards; appolo phase1 67 12 nims phase1 75 14 nims phase1 80 40 care phase1 34 10 care phase2 40 20 appolo phase2 267 22

nims phase2 178 14 care phase2 234 30 appolo phase1 245 50 appolo phase2 260 50 . 90 run; coalesce: can be used to replace the missing values for reporting. 1.numeric missing values we can replace using with numeric. 2.character missing values we can replace using with character. proc sql; select coalesce(center,'miss') as center,trail, coalesce(sub,0) as sub, adsub from clinical; quit; distinct(): can be used to report unique values from required variable. proc sql; select distinct(center) as centlist from clinical9; quit; /*Sequence*/ where group by having order by data clinical; infile cards truncover; input center $ trail $ sub adsub; cards; appolo phase1 67 12 nims phase1 75 14 nims phase1 80 40 care phase1 34 10 care phase2 40 20 appolo phase2 267 22 nims phase2 178 14 care phase2 234 30 appolo phase1 245 50

appolo phase2 260 50 . 90 run; Having clause: work like where clause if you want to do grouping analysis based on condition,we can use having clause. proc sql; create table x as select center,trail,sum(sub) as total from clinical group by center having center not in('care'); quit; proc sql; create table x1 as select center,trail,sum(sub) as total from clinical where center not in('care') group by center ; quit; count(): can be used to report frequency analysis. If we use * as argument in count function, it is report number of observation generated by current query statement. proc sql; select count(*) as norows from clinical9; quit; proc sql; select trail,count(trail) as obs from clinical9 group by trail; quit; proc sql; select center,trail, count(center) as obs from clinical9 group by center,trail; quit; proc sql; select center,trail, count(center),sum(sub) as obs

from clinical9 group by center,trail; quit; proc sql; select center,trail, count(center) as obs,sum(sub) as totsub from clinical9 group by center,trail; quit;

Views: views can be works like sas datasets. we can create a views from the existed sas datasets. data clinical; input center$ trail$ sub adsub; cards; appolo phase1 67 12 nims phase1 78 14 care phase1 34 10 appolo phase2 267 22 nims phase2 178 14 care phase2 234 40 appolo phase2 267 22 nims phase2 300 14 care phase2 200 40 run; proc sql; create view appolo as select * from clinical where center='appolo'; quit; proc print data=appolo;run; data one/view=option; set sashelp.class; where age<=14;run; proc print;run; proc sql; create view two as select * from sashelp.class;quit; proc print;run; /*describe statement:*/ can be used to report structure of the table or view.

proc sql; describe table sashelp.class;quit; proc contents data=sashelp.class;run; data emp; input eid salary sale; cards; 100 2000 500 101 3000 300 102 4000 600 104 4000 900 105 5000 400 106 6000 500 107 7000 400 108 8000 400 109 6000 300 run; proc sql; create table emp1 as select * from emp where sale>=500;quit; proc print;run; proc sql; create view emp2 as select * from emp;quit; proc sql; create view emp3 as select * from emp2 where sale>500;quit; proc print;run; proc sql; create table emp4 as select * from emp2 where sale<500;quit; proc print;run; data trt; input stno bsbp drug$ asbp; cards; 190 167 col5mg 178 123 178 col5mg 167 198 167 col10mg 146 237 172 col10mg 134 run; proc sql; select * from trt where bsbp>asbp; quit; data co; infile cards dsd;

input pid age gender$ race$; cards; 100 56 female asian 101 56 male african 34 female african 103 male asian 104 56 female asian 104 45 female asian run; proc print;run; proc sql; select * from sashelp.class; quit; proc sql; select name,sex from sashelp.class where name like 'J%' and sex='F' ; quit; proc sql; create table devi.class as select * from sashelp.class ; quit; proc print;run; proc sql; drop table devi.class; quit; proc sql; select sex,count(*) as count from sashelp.class group by sex order by count desc ; quit; proc sql; select weight, case when weight=. then '' when .<weight<80 then 'light' when 80<=weight<=110 then 'medium' else 'heavy' end as weight_class from sashelp.class; quit; proc sql; select sex,case sex when 'M' then 'boy' when 'F' then 'girl'

end as boy_or_girl from sashelp.class; quit; proc sql; select round(mean(height),0.01), round(mean(weight),0.01) into :avg_height_boys, :avg_weight_boys from sashelp.class where sex='M' ; quit; drop statement: can be used to drop the table or views from the sas environment. data emp; input eid salary sale; cards; 100 2000 500 101 3000 300 102 4000 600 104 4000 900 105 5000 400 106 6000 500 107 7000 400 108 8000 400 109 6000 300 run; proc sql; create table emp1 as select * from emp where sale>=500;quit; proc sql; drop table emp1; quit; /*subquery*/ data suplier; input scode $ sname $ addr $ ; cards; s01 raja hyd s02 rani sec s03 raghu hyd s04 radha ban run; data product; input pcode $ pname $ pcolor $; cards;

p1 item1 red p2 item2 green p3 item3 red p4 item4 blue run; data trasaction; input trno scode $ pcode $; cards; 1 s01 p3 2 s02 p1 3 s03 p4 4 s04 p2 5 s01 p4 6 s02 p3 7 s03 p3 8 s04 p1 6 s01 p2 run; /*list out all suppliers names who have supply product code'p2'*/ proc sql; create table sn as select scode,sname,addr from suplier where scode in (select scode from trasaction where pcode='p2'); quit; /*list out of the suppliers details who supplied red color products*/ proc sql; create table sd as select * from suplier where scode in( select scode from trasaction where pcode in( select pcode from product where pcolor='red')); quit; data x; input eid sal; cards; 100 2000 101 3000 102 5000 104 8900 99 1000 88 12000 run; /*Highest sal*/ proc sql; create table Highest as select max(sal) as highestsal from x; quit; /*second max sal*/ proc sql;

select max(sal) from x where sal< (select max(sal) from x); quit; proc sql; create table detail as select * from x where sal =( select max(sal) from x where sal< (select max(sal) from x)); quit; proc print;run; data x; input eid sal; cards; 100 2000 101 3000 102 5000 104 8900 99 1000 88 12000 run; /*Highest sal*/ proc sql; create table Highest as select max(sal) as highestsal from x; quit; /*second max sal*/ proc sql; select max(sal) from x where sal< (select max(sal) from x); quit; proc sql; create table detail as select * from x where sal =( select max(sal) from x where sal< (select max(sal) from x)); quit; proc print;run; /*Nth max sal*/ proc sql; SELECT DISTINCT (a.sal) FROM x A WHERE (5-1)= (SELECT COUNT (DISTINCT (b.sal)) FROM x B WHERE a.sal<=b.sal); quit;

/*Highest sal*/ proc sql; create table Highest as select max(sal) as highestsal from x; quit; /*second max sal*/ proc sql; select max(sal) from x where sal< (select max(sal) from x); quit; proc sql; create table detail as select * from x where sal =( select max(sal) from x where sal< (select max(sal) from x)); quit; proc print;run; /*Nth max sal*/ proc sql; SELECT DISTINCT (a.sal) FROM x A WHERE (5-1)= (SELECT COUNT (DISTINCT (b.sal)) FROM x B WHERE a.sal<=b.sal); quit; /*SQL pass through facility*/ /*Retrieved data from db2 table*/ proc sql noprint; connect to DB2 (database=XXXX user=xxxxxx password=xxxxxx); create table x as select * from connection to DB2 (select name,age,sal,loc from db2.emp order by name); disconnect from DB2; quit; 06/09/11.

MACROS
Using macros language, we can customize and reduce SAS language. Using macros language, we can develop reusable application. macro language is character based language. If we want to develop macro application in SAS, we need 2 requirements i. e one is macro compiler or processor and the second is macro language.

Macro language: I. This is one of part of the SAS. macro language: II. can be used to interact with macro processor. macro triggers(%,&): can be used to identify macro language. percentage(%):-This is called macro reference. Each and every macro statement starts with %. Ampson(&): This is called macro variable reference. It can be used for reporting macro variable. macro coding can be written outside and inside of the macro block. %macro <macroname>; SAS coding (include dataset block, proc block, open code) macro coding %mend; catalogue: whenever we run the macro application, SAS do compilation & stores compilation coding in catalogue. catalogue name same name of macro. Macro call: To call required macro for execution. Example: %macro pr; proc print;run; %mend; data x; input pid age; cards; 100 89 101 90 ; run; %pr; data x1; input pid drug $; cards; 100 5mg 101 6mg run; %let name1=ramu; %put &name; data x; input name $ sal; if name="&name1"; cards; ramu 10000 rani 20000

mahi 30000 raju 40000 run; %pr; %macro x(dsname,var1,var2); proc print data=&dsname; var &var1 &var2; run; %mend; %x(SAShelp.class,name,age); %x(SAShelp.shoes,product,region); concepts in macros: 1.macro variable creation 2.passing arguments to macro 3.macro quoting function 4.macro options 5.macro expressions 6.macro interface functions 1.macro variable creation: macro variables are 3 types 1.global macro variable 2.local macro variable 3.automatic macro variable All macro variable work like character. %let a=10; %let ab=20; %let c=%eval(&a+&b); %put &c;

07/09/11

Global macro variable can be created any in the program coding(inside/outside of macro coding). we can use any where program coding. %global statement: syntax: %global <macro variable>; %let statement: can be used to assign required value to macro variable.

or can be used to create user defined macro variable. %let name=prasad; %put name is &name; %let name=prasad; %macro cre; %global cname source; %let cname=raju; %let source=oracle; %let id=&name; %put &id; %mend; %cre; %put &cname; %put &source; %put statement: can be used to print required text and macro variable result in log window. Local macro varaiable: can be created inside of macro block and we can use in only in current macro block. local macro variable values stores in local symbol tables. %macro loc; %local cname1 source1; %let cname1=raju; %let source1=oracle; %put var1 is &cname1 and var2 is &source1; %mend; %loc; %put &cname1; %put &source1; Automatic macro variable creation: Automatic variables created by SAS and values assigned by SAS. It is a system defined.User can use automatic macro variables but user cannot reassign any vlue to automatic macro variables.Its values stores into global symbol table. %put &sysdate;

%put &systime; 2.passing arguments to macros: arguments:macro arguments should be written in after the macroname within brackets. %macro pr(dname); proc print data=&dname; run; %mend; %pr(SAShelp.clASS); %pr(SAShelp.shoes); we can pass the arguments in two ways. 1.postional paarameter or arguments. 2.keyword paarameter or arguments. 1.postional paarameter or arguments. can be used to pass the arguments based on position. %macro srt(ename,new,svar); proc sort data=&ename out=&new; by &svar; run; %mend; %srt(SAShelp.clASS,x,sex); data demo; input pid age gender $; cards; 100 34 male 101 23 female 102 45 male 101 45 male 101 45 male 102 34 male 103 56 female 100 34 male 101 23 female 102 45 male 101 45 male 101 45 male 101 45 male 102 34 male 103 56 female run; %macro dup(dname,ename,var); proc sort data=&dname out=&ename;

by &var; run; %mend; %dup(demo,dup1 nodupkey,pid); %macro dup(dname,ename,var); proc sort data=&dname out=&ename; by &var; run; %mend; %dup(demo,dupobs nodupkey,pid age gender); 2.keyword parameters: can be used to pass the arguments to macro using with parametrs or argument name. data demo; input pid age gender $; cards; 100 34 male 101 23 female 102 45 male 101 45 male 101 45 male 102 34 male 103 56 female 100 34 male 101 23 female 102 45 male 101 45 male 101 45 male 101 45 male 102 34 male 103 56 female run; %macro dup(dname=,ename=,var=); proc sort data=&dname out=&ename; by &var; run; %mend; %dup(dname=demo,ename=dupobs1 nodupkey,var=pid age gender); data demo; input pid age gender $; cards; 100 34 male 101 23 female

102 45 male 101 45 male 101 45 male 102 34 male 103 56 female 100 34 male 101 23 female 102 45 male 101 45 male 101 45 male 101 45 male 102 34 male 103 56 female run; %macro dup(dname=,ename=,var=); proc sort data=&dname out=&ename; by &var; run; %mend; %dup(ename=dupobs2 nodupkey,var=pid age gender,dname=demo);

08/09/11 To join 2 variables we need to add . ( dot ) in between. When to use single and double ampersand: During indirect references. %macro Ex2_refvar; %let mo = 12; %let yr1 = 2002; %let yr2 = 2003; %do y = 1 %to 2; %do day = 1 %to 2; %put &mo./&day./&yr&y; %put &mo./&day./&&yr&y; %end; %end; %mend Ex2_refvar; %Ex2_refvar; Macro quoting functions: %macro y; %let mo = December;

%let yr = 2002; %do day = 1 %to 3; %put &mo &day, yr; %put &mo &day, &yr.; %end; %mend; %y; %let yr=2002; %let day=13; %put &dayDecember&yr; %put &day.December&yr; %let a=&yr.&day; %put &a.; %let section1=internet; %let section2=networking; %let section3=operating system; %let section4=programing language; %let section5=web design; %let n=5; %put &&section&n; %let section1=internet; %let section2=networking; %let section3=operating system; %let section4=programing language; %let section5=web design; %let n=5; %let whatever=section; %put &&&whatever&n; %macro Ex2_refvar; %let mo = 12; %let yr1 = 2002; %let yr2 = 2003; %do y = 1 %to 2; %do day = 1 %to 2; %put &mo./&day./&yr&y; %put &mo./&day./&&yr&y; %end; %end; %mend Ex2_refvar; %Ex2_refvar; 3.macro quoting functions: Using with quoting functions we can

mask the special characters at compilation time. These are 2 types. %str(): using this function,we can mask all special characters except macro tiggers and unmatched quotations and unmatched brackets. %nrstr(): using this function,we can mask all special characters includes macro tiggers at compilation time. %macro subset(new,ename,con); data &new; set &ename; where &con; run; proc print;run; %mend; %subset(x,sashelp.class,age<=18); %subset(x,sashelp.class,%str(sex='F')); %subset(x2,sashelp.class, %nrstr(age>12 & name='Mary'));

%subset(x,sashelp.class,%str(name='Jeffrey')); %subset(x,sashelp.class,name='Jeffrey'); %print; %subset(x1,sashelp.class,age>12); %print; %subset(x2,sashelp.class,age>12 & name='Mary'); %let month=%substr(jan,feb,mar,5,3); %put &month; %let month=%substr(%str(jan,feb,mar),5,3); %put &month; %let reptxt=jan&feb %salereport; %put &reptxt; %let reptxt=%str(jan&feb %salereport); %put &reptxt;

%let reptxt=%nrstr(jan&feb %salereport); %put &reptxt; macro expression: are 3 types 1.text expression 2.Arithematic expression 3.logical expression 1.text expression:-macro coding is also called as text expression. 2.Arithematic expression: can be used to run arithematic operations in macros. %let a=10; %let b=20; %let c=&a+&b; %put &c; %eval(): can be used to do arithmatic operations using macro variables. %let c1=%eval(&a+&b); %put &c1; %let a=10; %let b=20; %let c=&a+&b; %put &c; %eval(): can be used to do arithmatic operations using macro variables. %let c1=%eval(&a+&b); %put &c1; %sysevalf(): If macro variables have period of characters(float values) then we will use sysevalf for arithmatic operation. %let a1=10.34; %let b1=20.60; %let s=%sysevalf(&a1+&b1); %put &s; Macro function (or) string function; It requires operands(variables). %length(): using this function,we can report length of macro variable.

%let dnames=demo lab med; %let len=%length(&dnames); %put &len; %index():using this function, we can report specific character position in string. %let dnames=demo lab med; %let pos=%index(&dnames,l); %put &pos; %scan(): using this function,we can get required word from string. or It extract nth word of string %let dnames=demo lab med; %let rw=%scan(&dnames,2); %put &rw; %upcase(): It shows required in capital letters. %let dnames=demo lab med; %let cap=%upcase(&dnames); %put &cap; %lowercase():It shows required in small letters. %substr(): we can get part of string from macro variable. %let dnames=demo lab med; %let sub=%substr(&dnames,1,5); %put &sub; %sysfunc():using this function, we can call dataset functions in macros. %let a=23.34; %let b=20.98; %let c=%sysevalf(&a+&b); %let in=%sysfunc(int(&c)); %put &c; %put &in; or %let a=23.34; %let b=20.98; %let c1=%sysfunc(int(%sysevalf(&a+&b))); %put &c1;

09/09/11 /*To develop merge application in macros*/ data main; input eid sal bonus; cards; 100 2000 .2 101 3000 .3 102 3500 .4 105 5000 .5 run; data tr; input eid dept $; cards; 100 testing 101 dwh 102 mainframe 103 testing 105 dwh 106 BI 108 BI run; %macro srt(ename,new,svar); proc sort data=&ename out=&new; by &svar; run; %mend; %macro merge(new,enames,mvar); data &new; merge &enames; by &mvar; run; %mend; %srt(main,y,eid); %srt(tr,y1,eid); %merge(mdata,%str(main(in=a) tr(in=b)), eid;if a=1 & b=1); %merge(ldata,%str(main(in=a) tr(in=b)), eid;if a=1); %merge(rdata,%str(main(in=a) tr(in=b)), eid;if b=1); %merge(fdata,%str(main(in=a) tr(in=b)), eid;if a=1 or b=1);

/*TO DEVELOP JOINS IN MACROS*/

%macro join(e1,e2,jo,con,mvar); proc sql; select * from &e1 &jo &e2 &con &e1..&mvar=&e2..&mvar; quit; %mend; %join(main,tr,%str(,),where,eid); %join(main,tr,left join,on,eid); %join(main,tr,right join,on,eid); data emp; input eid sal; cards; 100 2300 110 4500 230 5600 run; data emp2; input eid istage:percent4.; cards; 230 30% 110 20% 100 10% run; data emp3; input eid sal; cards; 108 2000 110 9000 run; %macro dmanage(rq,dname,enames,var); %if %upcase(&rq)=MERGE %then %do; data &dname; merge &enames; by &var; run; %end; %else %if %upcase(&rq)=UPDATE %then %do; data &dname; update &enames; by &var; run; %end; %else %if %upcase(&rq)=MODIFY %then %do; data &dname; modify &enames; by &var; run; %end; %else %do;

proc sort data=&dname out=&enames; by &var; %end; run; %mend; %dmanage( ,emp,z,eid); %dmanage( ,emp2,z1,eid); %dmanage( ,emp3,z4,eid); %dmanage(update,k,z z4,eid); %dmanage( modify,z,z z1,%str(eid;sal=sal+sal*istage)); data medi; input pid drug$ sbp; cards; 100 col5mg 156 101 col10mg 167 108 col10mg 145 102 col5mg 150 109 col10mg 160 run; data med; input pid drug$ sbp; cards; 100 col5mg 156 101 col10mg 167 run; data success; input pid sbp; cards; 101 145 100 150 run; %macro sprint(rq,ename,new,var); %if %upcase(&rq)=SORT %then %do; proc sort data=&ename out=&new; by &var; run; proc print data=&new; run; %end; %else %do; proc print data=&ename; run; %end; %mend; proc print data=medi1;run; %sprint(sort,medi,medi1,pid); %sprint(sort,med,med1,pid); %sprint(sort,success,success1,pid); %macro dmanage(rq,dname,enames,var); %if %upcase(&rq)=MERGE %then %do;

data &dname; merge &enames; by &var; run; %end; %else %if %upcase(&rq)=UPDATE %then %do; data &dname; update &enames; by &var; run; %end; %else %if %upcase(&rq)=MODIFY %then %do; data &dname; modify &enames; by &var; run; %end; %else %do; proc sort data=&enames out=&dname; by &var; run; %end; %mend; %macro print(dname); proc print data=&dname; run; %mend; /*%macro dmanage(rq,dname,enames,var);*/ %dmanage( ,medi2,medi,pid); %print(medi2); %dmanage( ,medi4,med,pid); %print(medi4); %dmanage( ,success1,success,pid); %print(success1); %dmanage(update,medi5,medi4 success1,pid); %dmanage(merge,medi6,medi2 medi4 success1,pid); %print(medi5); options mprint; %dmanage( ,medi1,medi,pid); %dmanage( ,success1,success,pid); %dmanage(update,medi2,medi1 success1,pid); %print(medi2); /*%DO %WHILE LOOP*/

%global dname; %let dname=emp1 emp2 emp3 emp4; %macro dh; %local dat i; %let i=1; %do %while(&i<=5); %let dat=%scan(&dname,&i); proc print data=&dat; run; %let i=%eval(&i+1); %end; %mend; %dh; options mprint mlogic symbolgen; %macro dh1; %local dat i; %let i=1; %let dat=%scan(&dname,&i); %do %while(&dat ne ); proc print data=&dat; run; %let i=%eval(&i+1); %let dat=%scan(&dname,&i); %end; %mend; %dh1; /*%DO %WHILE LOOP*/ data emp1; input eid sal; cards; 100 2000 101 3000 102 4000 103 5000 104 6000 105 8000 run; data emp2; input eid sal; cards; 200 2000 201 3000 202 4000 203 5000 204 6000 205 8000 run; data emp3; input eid sal;

cards; 300 2000 301 3000 302 4000 303 5000 304 6000 305 8000 run; data emp4; input eid sal; cards; 400 2000 401 3000 402 4000 403 5000 404 6000 405 8000 run; %global dname; %let dname=emp1 emp2 emp3 emp4 emp5 emp6; %macro dh; %local dat i; %let i=1; %do %while(&i<=7); %let dat=%scan(&dname,&i); proc print data=&dat; run; %let i=%eval(&i+1); %end; %mend; %dh; options mprint mlogic symbolgen; %macro dh1; %local dat i; %let i=1; %let dat=%scan(&dname,&i); %do %while(&dat ne ); proc print data=&dat; run; %let i=%eval(&i+1); %let dat=%scan(&dname,&i); %end; %mend; %dh1; Macro options: Macro options is a type of global options. Its deault working whenever we run the macro application. macro options can be changed by using option statement.This statement

should be written outside of the macro block. It displays a warning message in log window whenever macro call is not resolved. Merror: using this option,we can trace out required catalogue(macro call) existed or not. Serror: prints warning message in log window whenever macro variable is not resolved. mprint: Using mprint option,we can trace out required macrocall to report errors in sas coding. symbolegen: can be used to trace out macro variable value. or It prints message in log window how to resolve macro variable. Mlogic: can be used to trace out logical expressions.

/*%DO %WHILE LOOP*/ data emp1; input eid sal; cards; 100 2000 101 3000 102 4000 103 5000 104 6000 105 8000 run; data emp2; input eid sal; cards; 200 2000 201 3000 202 4000 203 5000 204 6000 205 8000 run;

data emp3; input eid sal; cards; 300 2000 301 3000 302 4000 303 5000 304 6000 305 8000 run; data emp4; input eid sal; cards; 400 2000 401 3000 402 4000 403 5000 404 6000 405 8000 run; %global dname; %let dname=emp1 emp2 emp3 emp4 emp5 emp6; options mprint mlogic symbolgen; %macro dh1; %local dat i; %let i=1; %let dat=%scan(&dname,&i); %do %while(&dat ne ); proc print data=&dat; run; %let i=%eval(&i+1); %let dat=%scan(&dname,&i); %end; %mend; %dh1; 10/09/11 In macros we need to put . (dot) to add names. http://www.stat.berkeley.edu/classes/s100/sas.pdf Concatination of macro variables: %let surname=kolla; %let name=lava kumar; %let concat=&surname.&name.; %put &concat; GO TO BLOCK: The statement is working based on label statement and run group of required statements.

label statement: this statemnt indicate group of statements. If we want to run 'go to' statement we will use conditional if. data x; input eid sale; cards; 100 2000 101 3000 102 4000 104 5678 105 7890 106 5678 107 8908 108 4567 109 8654 110 9000 run; data x1; set x; if 2000<=sale <=3000 then goto la1; else if 3000<sale <=5000 then goto la2; else if sale> 5000 then goto la3; la1: salary=2000; bonus=salary*0.5; netsalary=salary+bonus; return; la2: salary=3000; bonus=salary*0.5; netsalary=salary+bonus; return; la3: salary=4000; bonus=salary*0.5; netsalary=salary+bonus; return; run; data x; input eid sale; cards; 100 2000 101 3000 102 4000 104 5678 105 7890 106 5678

107 8908 108 4567 109 8654 110 9000 run; data x2; set x; if 2000<=sale <=3000 then do; salary=2000; bonus=salary*0.5; netsalary=salary+bonus; end; else if 3000<sale <=5000 then do; salary=3000; bonus=salary*0.5; netsalary=salary+bonus; end; else if sale> 5000 then do; salary=4000; bonus=salary*0.5; netsalary=salary+bonus; end; run; data emp; input eid sal; cards; 100 2000 101 3000 102 4000 104 5678 105 7890 106 5678 run; data dept; input eid loc $; cards; 102 chicago 101 delhi 106 hyd 104 paris 105 newyork 100 london run; %macro gto(rq,dname,enames,var); %if %upcase(&rq)=SORT %then %goto srt; %else %if %upcase(&rq)=MERGE %then %goto merge; %srt: proc sort data=&enames out=&dname;

by &var; run; %goto ext; %merge: data &dname; merge &enames; by &var; run; %goto ext; %ext:%mend; %gto(sort,emps,emp,eid); %gto(sort,depts,dept,eid); %gto(merge,mr,emps depts,eid); Macro interface functions:Interface functions are 2 types. 1.Dataset interface functions(datastep). 2.macro interface function. Dataset interface functions: call symput:It is a call routine(function). Using this function,we can create macro variables from the dataset variables during dataset execution. Syntax:call symput("macro variable",dataset varname); Note: If dataset has mutiple values call symput function default stores last data values in macro variable. data x; input name $ sal; call symput("name1",name); cards; raju 2000 mahi 4000 suri 5000 run; %put &name1; data x; input name $ sal; CALL SYMPUT('v'||LEFT(_N_), name); cards; raju 2000 mahi 4000 suri 5000 ; run; %put &v1; %put &v2;

%put &v3; data x; input name $ sal; anual+sal; cards; raju 2000 mahi 4000 suri 5000 ; run; DATA _NULL_; SET x END=LAST; IF LAST THEN CALL SYMPUT('N',anual); RUN; %put &n; Symget: If want to get values of macro variable at datastep level. data x; input name $ sal; anual+sal; cards; raju 2000 mahi 4000 suri 5000 ; run; DATA _NULL_; SET x END=LAST; IF LAST THEN CALL SYMPUT('N',anual); RUN; data x2; anualsal=symget('n'); run; call execute:using call execute,we can call required catalog(macro call) from the dataset block. syntax: call execute('%macro call'); data x; input name $ sal; anual+sal; cards; raju 2000 mahi 4000 suri 5000

; run; DATA _NULL_; SET x END=eof; IF eof THEN CALL execute('%gto(merge,mr2,emps depts,eid)'); RUN; 2.macro interface function. %sysfunc():using this function,we can call data set functions in macros. %let a=20.23; %let b=34.78; %let c=%sysfunc(int(%sysevalf(&a+&b))); %put &c; dataset function: exist():Using this function,we can report required sas file is existed or not. If it is existed it returns 1 otherwise 0. syntax:Exist('datasetname'); data _null_; if exist('emp')=1 then put 'dataset is existed'; else put 'dataset does not exist'; run; open():Using this function,we can open dataset internally. syntax: open('datasetname'); Attrn():Using this function, we can count number of rows and number og variables using open results. close():Using this function,we can close open dataset. syntax: close(open datasetname); data _null_; if exist('sashelp.class')=1 then do; op=open('sashelp.class'); NV=attrn(op,'nvars'); NO=attrn(op,'nobs'); CL=close(op); put 'no of observation' no;

put 'no of variables ' nv; end; else put 'dataset not existed'; run; %macro dex(dname); %if %sysfunc(exist(&dname))=1 %then %do; %let op=%sysfunc(open(&dname)); %let NV=%sysfunc(attrn(&op,nvars)); %let NO=%sysfunc(attrn(&op,nobs)); %let CL=%sysfunc(close(&op)); %put no of observation &no; %put no of variables &nv; %end; %else %put &dname not existed; %mend; %dex(sashelp.class); %dex(sashelp.shoes); /*To create a macro variable with sql block*/ select and into clause, using these two options we can create macro variable the dataset variable. Here sas system default stores 1st datavalue or first occurance in macro variable. data med; input gid $ visit drug $; cards; G100 1 col105mg G200 1 col10mg g300 1 col15mg G100 2 col28mg G200 2 col30mg g300 2 col20mg run; proc sql noprint; select drug into:medicine from med; quit; %put &medicine; /*To create multiable macro variables*/ proc sql noprint; select drug into:medicine1-:medicine6 from med; quit; %put &medicine1; %put &medicine2; %put &medicine3; %put &medicine4; %put &medicine5; %put &medicine6;

proc sql noprint; select count(*) into:n from med; select drug into:medicine1-:medicine%sysfunc(left(&n)) from med; quit; %put &n; %put &medicine1; %put &medicine2; %put &medicine3; %put &medicine4; %put &medicine5; %put &medicine6; %put &medicine7; symdel:Using this function, we can delete macro variable from sas environment. %symdel medicine7; _Global_:Using _global_ statement , we can report list the macro variables(global variables) with values. %put _global_; _local_:Using _local_ statement , we can report list the macro variables(local variables) with values. %macro mvar; %let a=10; %let b=20; %let c=%eval(&a+&b); %put _local_; %mend; %mvar; _user_: Using this statement, we can report user defined macro variable. 1.if we write inside of macroblock, it reports both global and local macro variables. 2.if we write outside of macroblock, it reports both global macro variables. %put _user_; _automatic_: Using this statement,

we can report list of automatic macro variables. %put _automatic_; %put _automatic_; %put &sysdate; %put &systime; %put &sysdsn;

Concatination of macro variables: %let surname=kolla; %let name=lava kumar; %let concat=&surname.&name.; %put &concat; GO TO BLOCK: The statement is working based on label statement and run group of required statements. label statement: this statemnt indicate group of statements. If we want to run 'go to' statement we will use conditional if. data x; input eid sale; cards; 100 2000 101 3000 102 4000 104 5678 105 7890 106 5678 107 8908 108 4567 109 8654 110 9000 run; data x1;

set x; if 2000<=sale <=3000 then goto la1; else if 3000<sale <=5000 then goto la2; else if sale> 5000 then goto la3; la1: salary=2000; bonus=salary*0.5; netsalary=salary+bonus; return; la2: salary=3000; bonus=salary*0.5; netsalary=salary+bonus; return; la3: salary=4000; bonus=salary*0.5; netsalary=salary+bonus; return; run; data x; input eid sale; cards; 100 2000 101 3000 102 4000 104 5678 105 7890 106 5678 107 8908 108 4567 109 8654 110 9000 run; data x2; set x; if 2000<=sale <=3000 then do; salary=2000; bonus=salary*0.5; netsalary=salary+bonus; end; else if 3000<sale <=5000 then do; salary=3000; bonus=salary*0.5; netsalary=salary+bonus; end;

else if sale> 5000 then do; salary=4000; bonus=salary*0.5; netsalary=salary+bonus; end; run; data emp; input eid sal; cards; 100 2000 101 3000 102 4000 104 5678 105 7890 106 5678 run; data dept; input eid loc $; cards; 102 chicago 101 delhi 106 hyd 104 paris 105 newyork 100 london run; %macro gto(rq,dname,enames,var); %if %upcase(&rq)=SORT %then %goto srt; %else %if %upcase(&rq)=MERGE %then %goto merge; %srt: proc sort data=&enames out=&dname; by &var; run; %goto ext; %merge: data &dname; merge &enames; by &var; run; %goto ext; %ext:%mend; %gto(sort,emps,emp,eid); %gto(sort,depts,dept,eid); %gto(merge,mr,emps depts,eid); Macro interface functions:Interface functions are 2 types. 1.Dataset interface functions(datastep). 2.macro interface function.

Dataset interface functions: call symput:It is a call routine(function). Using this function,we can create macro variables from the dataset variables during dataset execution. Syntax:call symput("macro variable",dataset varname); Note: If dataset has mutiple values call symput function default stores last data values in macro variable. data x; input name $ sal; call symput("name1",name); cards; raju 2000 mahi 4000 suri 5000 run; %put &name1; data x; input name $ sal; CALL SYMPUT('v'||LEFT(_N_), name); cards; raju 2000 mahi 4000 suri 5000 ; run; %put &v1; %put &v2; %put &v3; 11/09/11 %macro mvar; %let a=10; %let b=20; %let c=%eval(&a+&b); %put _local_; %mend; %mvar; data multdat; input id $ date mmddyy10. pr1 pr2 pr3 t1 t2 t3; format date mmddyy10.; cards; A1 11/01/2004 223 204 195 30 28 27

B7 11/01/2004 211 192 183 31 28 26 ; run; proc print;run; data unidat; set multdat; keep id date time pressure temp; time=1;pressure=pr1;temp=t1;;output; time=2;pressure=pr2;temp=t2;;output; time=3;pressure=pr3;temp=t3;;output; run; proc print;run; data uni; format date mmddyy10.; input id $ date mmddyy10. @; time=1; input pressure temp @;output; time=2; input pressure temp @;output; time=3; input pressure temp @;output; cards; A1 11/01/2004 223 204 195 30 28 27 B7 11/01/2004 211 192 183 31 28 26 ; run; proc print;run; data r; x=22/7; idvar='a'; y1=round(x,1); y_1=round(x,0.1); y_01=round(x,0.01); output; x=33/7; idvar='b'; y1=round(x,1); y_1=round(x,0.1); y_01=round(x,0.01); output; run; proc transpose data=r out=tr; var x y1 y_1 y_01; run; proc print;run; proc transpose data=r out=tr name=orig_var prefix=y_;

var x y1 y_1 y_01; id idvar; run; proc print;run; proc sort data=multdat out=x; by id date; run; proc transpose data=x out=p(drop=_name_ rename=(col1=p)); by id date; var pr1 pr2 pr3; run; proc transpose data=x out=t(drop=_name_ rename=(col1=t));; by id date; var t1 t2 t3; run; data um; merge p t; by id date; run; proc print;run; Proc transpose: Using this procedure,we can convert variables into rows and rows into variables. Id statement: it requires which data variable values to tranpose or convert as variables. var statement: It requires which variable values to convert or transpose as observation or datavalue. prefix; can be used to add required text for tranpose variable. data lab; input id test $ units; cards; 100 hr 90 101 hr 89 100 dbp 98 101 dbp 97 100 sbp 156 101 sbp 167 run; proc sort data=lab out=x;

by id; run; proc transpose data=x out=tr1 name=details prefix=test_; by id; var units; id test; run; proc print data=tr1;run;

Name:gives name to new variable that contains the name of tranposed variables (the variables listed on var statement). if you donot enter this option,sas automatically includes variable called _name_. You can drop it with(drop=_name_) placed immediately following out=<tr_dataset_name> or enter the rename=(_name_=new_name) option. prefix: provides initial characters for the names of the new variables that will be appended with the value of variable listed on ID statement,such as prefix=p will list new variables as p1,p2,p3..... sometimes _ is convenient choice for the first character of the tranposed variable. 13/09/11 Proc datasets proc datasets: using this procedure we can do 1.Rename the datasets 2.Exchange data between the datsets. 3.copy the sas files from one libray to another library. 4.modify the datasets a) aasiagn constraints b)delete constraints 5.Append the datasets values from one dataset to another. 6. we can report descriptive information for required library. 1.Rename the datasets change statement:can be used to rename the dataset. data emp;

input eid sal jod:ddmmyy10.; cards; 100 2000 30-01-2010 101 3000 23-02-2009 103 3500 25-03-2008 run; proc datasets lib=work; change emp=employee; quit; 2.Exchange data between the datsets. exchange statement: can be used to exchange between the datasets. Note: If we want to exchange the data between the sas file,then 2 sas files must be available in same library. data emp1; input eid sal jod:ddmmyy10.; cards; 100 2000 30-01-2010 101 3000 23-02-2009 103 3500 25-03-2008 run; proc datasets lib=work; exchange employee=emp1; quit; 3.copy the sas files from one libray to another library. copy statement: can be used to copy the sas files between the libraries or we can transfer sas files between libraries. proc datasets lib=sashelp; copy in=sashelp out=work; quit; proc datasets lib=sashelp; copy in=sashelp out=work; select class shoes; quit; memtype option: can be used to copy the required sas file type or required memtype. values of memtypes are: All,data,view,cat.

we can copy the required sas files use with select and exclude statement. select statement indicates required sas files. exclude statement indicates non-required sas files. proc datasets lib=sashelp; copy in=sashelp out=work memtype=data; select class shoes; quit; proc datasets lib=sashelp; copy in=sashelp out=work memtype=cat; quit; proc datasets lib=sashelp; copy in=sashelp out=work memtype=view; quit; proc datasets lib=sashelp; copy in=sashelp out=work memtype=data; exclude class shoes; quit; 14/09/11 4.modify the datasets: modify statement can be used to change the formats. data emp; input eid sal jod:ddmmyy10.; format jod date9.; cards; 100 2000 30-01-2010 101 3000 23-02-2009 103 3500 25-03-2008 run; proc datasets lib=work; modify emp; format jod worddate20.; quit; proc print;run; proc datasets lib=work; modify emp; format jod worddate20.; rename jod =joindate; quit; proc print;run; data emp; input eid sal jod:ddmmyy10.; format jod date9.; cards;

100 2000 30-01-2010 101 3000 23-02-2009 103 3500 25-03-2008 run; proc datasets; modify emp; format jod weekdate22.; rename jod =joindate; quit; proc print;run; 5.Appending statement: using append statement, we can append the values or load the values from one dataset to other. Append statement can be used only in data set procedure. data x; input a b; cards; 10 45 34 56 run; data y; input a b; cards; 100 350 120 567 run; proc datasets ; append base=x data=y; quit; data x; input a b ; cards; 10 45 34 56 run; data y; input a b c; cards; 100 350 45 120 567 789 run; proc datasets ; append base=x data=y force; quit; data x; input a b d; cards;

10 45 300 34 56 450 run; data y; input a b c; cards; 100 350 45 120 567 789 run; proc datasets ; append base=x data=y force; quit; 6.To report descriptive information for required library. Details options:can be used to report descriptive information for required library. proc datasets lib=work details; quit; contents statement: To report descriptive information for required dataset. proc datasets lib=work; contents data=emp; quit; Delete statemnt: delete required dataset from library. proc datasets lib=work; delete emp; quit; 17/09/11 To get data in pyramid shape: Data _Null_ ; Length Text $ 200 ; Max = 10 ; Do I = 1 To Max ; Text = Repeat( Strip(Put( I , Best32. )) || ' ' , I - 1 ) ; Put Text; End ; Run ; constraints: can be used to load necessary data in tables. we can assign constraints in 2 ways. 1.column constraints 2.table constraints

integrity constraints types: Unique: can be used to load the data without duplicate data values. proc sql; create table demo(pid num unique,age num, gender char,race char); quit; proc sql; insert into demo values(100,21,"f","asian") values(101,22,"m","african") values(102,23,"f","mangoli") values(100,23,"f","mangoli"); quit; proc sql; describe table demo; quit; not null: can be used to load the data without missing values(numeric type and character type). proc sql; create table demo(pid num not null,age num, gender char,race char); quit; proc sql; insert into demo values(100,21,"f","asian") values(101,22,"m","african") values(102,23,"f","mangoli") values(100,23,"f","mangoli") values(100,23,"f","mangoli"); quit; proc sql; describe table demo; quit; check: can be used to load the data based on condition. proc sql; create table demo(pid num,age num, gender char check(gender="f"),race char ); quit; proc sql; insert into demo values(100,21,"f","asian") values(101,22,"f","african")

values(102,23,"f","mangoli") values(100,23,"f","mangoli") values(100,23,"f","mangoli") ; quit; proc sql; create table demo(pid num,age num, gender char check(gender="f"),race char ); quit; proc sql; insert into demo values(100,21,"f","asian") values(101,22,"m","african") values(102,23,"f","mangoli") values(100,23,"f","mangoli") values(100,23,"f","mangoli") ; quit; /*proc sql;*/ /*describe table demo;*/ /*quit;*/ /*proc sql;*/ /*describe table demo;*/ /*quit;*/ primary key: can be used to load the data without duplicate data values and duplicate observations and without missing values. Table constraints: using table constraint,we can avoid duplicate observation. primary key: Using primary key as a table constraint,we can avoid duplicate observations in loading time. constraint statement: can be used to assign required constraint for required variable. syntax: constraint <integrityname> <type> <variable> proc sql; create table demo1(pid num,age num, gender char ,race char, constraint pk primary key(pid)); quit;

proc sql; describe table demo1; quit; proc sql; insert into demo1 values(100,21,"f","asian") values(101,22,"f","african") values(102,23,"f","mangoli") ; quit; proc sql; create table demo1(pid num,age num, gender char ,race char, constraint pk primary key(pid)); quit; proc sql; describe table demo1; quit; proc sql; insert into demo1 values(100,21,"f","asian") values(101,22,"f","african") values(102,23,"f","mangoli") values(100,23,"f","mangoli") values(100,23,"f","mangoli") ; quit; proc sql; create table demo1(pid num,age num, gender char ,race char, constraint pk primary key(pid)); quit; proc sql; insert into demo1 values(100,21,"f","asian") values(101,22,"f","african") values(102,23,"f","mangoli") ; quit; proc sql; create table lab (pid num,test char,units num, constraint fk foreign key(pid) references demo1); quit; proc sql; insert into lab values(100,'hr',76) values(101,'sbp',156); quit; proc append base=demo1 data=lab force;

run; data x; set demo1 lab; run; 19/09/11 proc sql; create table demo(pid num ,age num, gender char,race char); quit; proc datasets lib=work; modify demo; ic create uk=unique(pid); quit; proc sql; describe table demo; quit; proc sql; insert into demo values(100,21,"f","asian") values(101,22,"m","african") values(102,23,"f","mangoli") ; quit; proc datasets lib=work; modify demo; ic create nt=not null(gender); ic create ck=check(where=(gender='f')); quit; proc sql; insert into demo values(100,21,"f","asian") values(101,22,"f","african") values(102,23,"f","mangoli") values(103,23,"f","mangoli") ; quit; proc sql; insert into demo values(100,21,"f","asian") values(101,22,"f","african") values(102,23,"f","mangoli") values(103,23,"f","mangoli") values(104,23,"m","mangoli"); quit; proc report: if we want columns use column

Result :

data _null_; file 'C:\Users\home\Desktop\New folder (3)\uk.txt'; put @1 'patientname' @18 'medicine' @28 'No of visits' @43 'No of patients'; run; proc sort data=trtment out=x; by visit;run; data _null_; set x; by visit; file 'C:\Users\home \Desktop\New folder (3)\uk.txt' mod; put @1 gid @22 drug @38 visit @56 sub; if first.visit then ct=0; ct+sub; if last.visit then put @58 'visit' visit 'total' ct; run; 20/09/11 proc report: using this procedure,we can generate required analysis and generate a reports in required format.This is powerful reporting tool. using this procedure,we can do frequency proceure analysis,mean procedure analysis,tabulate procedure analysis and print procedure analysis. Report window:report procedure generate report in report window. columns statement:

It require variable list and these variables playing a main role in analysis and report. Define statement: can be used to indicate sas system, how to use required variable in analysis and reporting. order,group,across options: The main use of these options is to arrange the data in required order for reporting. break statement: can be used to give break summary breaks in middle of the reports based on group variable.Break statement is working on 2 options. 1).After:-It indicate to give the break after grouping. 2).Before:-It indicate to give the break before grouping. ol-overline ul-underline dol-double overline dul-double underline summarize option:It can be used to report the required analysis. Rbreak statement:can be used to give summary break end of the report or begining of the report based on after or before options. compute block: using this block,we can do new analysis for reporting. 1.To generate new data value for reporting. 2.To create new variable for reporting. compute block ends with endcomp. compute block also working based on after and before options. data boats; input name$1-12 port $ 14-20 locomotion $ 22-26 type $ 28-30 price 32-36; cards; silent lady maalea sail sch 75.00 american II maalea sail yac 32.95 aloha anai lahaina sail cat 62.00 ocean spirit maalea power cat 22.00 anuenue maalea sail sch 47.50 hana lei maalea power cat 28.99 leilani maalea power yac 19.99 kalakaua maalea power cat 29.50

reef runner lahaina power yac 29.95 blue dolphin maalea sail cat 42.95 run; data natparks; input name $ 1-21 type $ region $ museums camping; cards; dinosaur nm west 2 6 ellis island nm east 1 0 everglades np east 5 2 grand canyon np west 5 3 great smoky mountains np east 3 10 hawaii volcanoes np west 2 2 lava beds nm west 1 1 statue of liberty nm east 1 0 theodore roosevelt np . 2 2 yellowstone np west 9 11 yosemite np west 2 13 run;proc print;run; proc report data=natparks nowd headskip; run; proc report data=natparks nowindows headline; column museums camping; run; proc report data=natparks nowindows headline; column museums camping; define museums/display analysis sum; define camping/display analysis sum; run; proc report data=natparks nowindows headline; column museums camping; define museums/display analysis mean; define camping/display analysis mean; run; proc report data=natparks nowindows headline missing; column region name museums camping; define region/order; define camping/analysis 'camp/grounds'; run; proc report data=natparks nowindows headline missing; column region type museums camping; define region/group; define camping/group; run; proc report data=natparks nowindows headline missing; column region type,( museums camping); define region/group; define type/across;run; proc report data=natparks nowindows headline missing; column name region museums camping; define region/order;

break after region/summarize ol skip; rbreak after/summarize ol skip; run; data trtment; input gid$ drug$ visit sub; cards; g1234 col5mg 1 90 g2345 col5mg 1 89 g4567 col5mg 1 78 g1234 col5mg 2 50 g2345 col6mg 2 79 g4567 col6mg 2 38 g1234 col6mg 3 70 g2345 col6mg 3 89 g1234 col7mg 4 90 g2345 col7mg 4 89 run; proc report data=trtment nowindows; column gid drug sub; define gid/group; define sub/sum; break after gid/ol ul summarize; rbreak after/dol dul summarize; compute after gid; gid ='total'; endcomp; compute after; gid='gtotal'; endcomp; run; data x; input eid sal; cards; 100 2000 101 2500 102 3000 run; proc report data=x nowd; columns eid sal ; define eid/'employeeid' width=10 display ; define sal/'empsalary' width=10; run; proc report data=x nowd; columns eid sal anualsal ; define eid/'employeeid' width=10 display ; define sal/'empsalary' width=10 display; define anualsal/ 'anualsal' computed; compute anualsal; anualsal=sal*12; endcomp;

run; data company; input cname$ details$ amount; cards; satyam invest 6700 tcs invest 6800 satyam profit 3400 tcs profit 2300 wipro invest 5600 wipro profit 3400 run; proc report data=company headline nowindows; columns cname(details,amount); define cname/group; define details/across; break after cname/dul; run; data medi; input pid bsbp drug$ asbp; cards; 100 178 col5mg 165 101 156 col5mg 159 102 178 col10mg 168 103 177 col10mg 177 104 180 col15mg 182 105 169 col15mg 134 run; proc report data=medi headline; columns pid drug bsbp asbp status; define pid/group; define status/computed; break after pid/ol ul; compute status/character length=19; if _c3_ < _c4_ then status='drug is not working'; else if _c3_ > _c4_ then status='drug is working'; else status='change the drug'; endcomp; run;

proc report: using this procedure,we can generate required analysis and generate a reports in required format.This is powerful reporting tool. using this procedure,we can do frequency proceure analysis,mean procedure analysis,tabulate procedure analysis and print procedure analysis. Report window:report procedure generate report in report window. columns statement:

It require variable list and these variables playing a main role in analysis and report. Define statement: can be used to indicate sas system, how to use required variable in analysis and reporting. order,group,across options: The main use of these options is to arrange the data in required order for reporting. break statement: can be used to give break summary breaks in middle of the reports based on group variable.Break statement is working on 2 options. 1).After:-It indicate to give the break after grouping. 2).Before:-It indicate to give the break before grouping. ol-overline ul-underline dol-double overline dul-double underline summarize option:It can be used to report the required analysis. Rbreak statement:can be used to give summary break end of the report or begining of the report based on after or before options. compute block: using this block,we can do new analysis for reporting. 1.To generate new data value for reporting. 2.To create new variable for reporting. compute block ends with endcomp. compute block also working based on after and before options. data boats; input name$1-12 port $ 14-20 locomotion $ 22-26 type $ 28-30 price 32-36; cards; silent lady maalea sail sch 75.00 american II maalea sail yac 32.95 aloha anai lahaina sail cat 62.00 ocean spirit maalea power cat 22.00 anuenue maalea sail sch 47.50 hana lei maalea power cat 28.99 leilani maalea power yac 19.99 kalakaua maalea power cat 29.50

reef runner lahaina power yac 29.95 blue dolphin maalea sail cat 42.95 run; data natparks; input name $ 1-21 type $ region $ museums camping; cards; dinosaur nm west 2 6 ellis island nm east 1 0 everglades np east 5 2 grand canyon np west 5 3 great smoky mountains np east 3 10 hawaii volcanoes np west 2 2 lava beds nm west 1 1 statue of liberty nm east 1 0 theodore roosevelt np . 2 2 yellowstone np west 9 11 yosemite np west 2 13 run;proc print;run; proc report data=natparks nowd headskip; run; proc report data=natparks nowindows headline; column museums camping; run; proc report data=natparks nowindows headline; column museums camping; define museums/display analysis sum; define camping/display analysis sum; run; proc report data=natparks nowindows headline; column museums camping; define museums/display analysis mean; define camping/display analysis mean; run; proc report data=natparks nowindows headline missing; column region name museums camping; define region/order; define camping/analysis 'camp/grounds'; run; proc report data=natparks nowindows headline missing; column region type museums camping; define region/group; define camping/group; run; proc report data=natparks nowindows headline missing; column region type,( museums camping); define region/group; define type/across;run; proc report data=natparks nowindows headline missing; column name region museums camping; define region/order;

break after region/summarize ol skip; rbreak after/summarize ol skip; run; data trtment; input gid$ drug$ visit sub; cards; g1234 col5mg 1 90 g2345 col5mg 1 89 g4567 col5mg 1 78 g1234 col5mg 2 50 g2345 col6mg 2 79 g4567 col6mg 2 38 g1234 col6mg 3 70 g2345 col6mg 3 89 g1234 col7mg 4 90 g2345 col7mg 4 89 run; proc report data=trtment nowindows; column gid drug sub; define gid/group; define sub/sum; break after gid/ol ul summarize; rbreak after/dol dul summarize; compute after gid; gid ='total'; endcomp; compute after; gid='gtotal'; endcomp; run; data x; input eid sal; cards; 100 2000 101 2500 102 3000 run; proc report data=x nowd; columns eid sal ; define eid/'employeeid' width=10 display ; define sal/'empsalary' width=10; run; proc report data=x nowd; columns eid sal anualsal ; define eid/'employeeid' width=10 display ; define sal/'empsalary' width=10 display; define anualsal/ 'anualsal' computed; compute anualsal; anualsal=sal*12; endcomp;

run; data company; input cname$ details$ amount; cards; satyam invest 6700 tcs invest 6800 satyam profit 3400 tcs profit 2300 wipro invest 5600 wipro profit 3400 run; proc report data=company headline nowindows; columns cname(details,amount); define cname/group; define details/across; break after cname/dul; run; data medi; input pid bsbp drug$ asbp; cards; 100 178 col5mg 165 101 156 col5mg 159 102 178 col10mg 168 103 177 col10mg 177 104 180 col15mg 182 105 169 col15mg 134 run; proc report data=medi headline; columns pid drug bsbp asbp status; define pid/group; define status/computed; break after pid/ol ul; compute status/character length=19; if _c3_ < _c4_ then status='drug is not working'; else if _c3_ > _c4_ then status='drug is working'; else status='change the drug'; endcomp; run; Task 1: data x; input eid sal; cards; 100 2000 100 2500 101 2500

101 2500 100 2000 100 2000 100 2000 101 2500 102 3000 103 4500 110 5000 105 5000 108 7000 109 4000 110 3000 111 4000 112 8000 113 5500 run; Create new dataset containing duplicate obs proc sort data=x out=dups; by eid; run; data x2; set dups; by eid; if not (first.eid and last.eid); run; proc print;run or ( new ) proc sort data=x out=x1 nodupkey dupout=dups; by eid; run; Output Delivery system ods pdf file='C:\Users\home\Desktop \New folder (3)/uk.pdf'; proc report data=sashelp.class nowd; run; ods pdf close; ods html file='C:\Users\home\Desktop \New folder (3)/uk.html'; proc report data=sashelp.class nowd; run; ods html close; ods rtf file='C:\Users\home\Desktop \New folder (3)/uk.rtf';

proc report data=sashelp.class nowd; run; ods rtf close; Data x; input cust $ name $ age sex $ proc $ charge hrp_charge rent_charge; cards; 100 raja 24 M 890 900 1000 2000 200 kaja 67 M 900 987 2000 8000 100 rani 89 m 300 800 600 876 300 hani 56 F 800 908 123 8908 run; PROC SQL; CREATE TABLE x1 AS SELECT *, PUT(SUM(CHARGES),BEST.) AS CHARGES1, PUT(SUM(HRP_ALLOW),BEST.) AS HRP_ALLOW1, PUT(SUM(PPOALLOW),BEST.) AS PPOALLOW1 FROM x GROUP BY cust; QUIT; PROC SQL; CREATE TABLE x2 AS SELECT *,SUM(CHARGES) AS CHARGES2 , SUM(HRP_ALLOW) AS HRP_ALLOW2 , SUM(PPOALLOW) AS PPOALLOW2 FROM x1; QUIT;

DATA TEMP1_REP; length key1 $150. key2 $150. KEY3 $150. KEY4 $150. KEY5 $150.; SET x2; DOB_DATE1=put(input(DOB_DATE,anydtdte19.),mmddyys10.); ADATE1=put(input(ADATE,anydtdte19.),mmddyys10.); KEY1=CAT(cust,'^2',NAME,'^3',sex); KEY2=CAT(proc,'0'); KEY3=CAT(charge,'^2',hrp_charge,'^3',rent_charge); KEY4=CAT(cust,'^2',CHARGES,'^3',HRP_ALLOW,'^4',PPOALLOW); RUN; options papersize=letter mlogic mprint symbolgen nodate nonumber; footnote; ods pdf file='C:\Users\home\Desktop \New folder (3)/uk2.pdf' UNIFORM notoc; /*ods pdf option UNIFORM;*/ proc report data=x2;

COLUMNS KEY1 KEY2 KEY3 KEY4 KEY5 KEY6; define KEY1 /GROUP define KEY2 /GROUP define KEY3 /GROUP define KEY4 /GROUP define KEY5 /GROUP define KEY6 /GROUP ' ' noprint style(column)={cellwidth=2 just=left}; ' ' noprint; ' ' noprint; ' ' noprint; ' ' noprint; ' ' noprint;

break before KEY1 / SKIP; compute before KEY1 / style={just=left}; cust=Substr(KEY1,1,index(key1,'^2')-1); NAME=Substr(KEY1,index(key1,'^2')+2,(index(key1,'^3')-index(key1,'^2'))-2); SEX=substr(key1,index(key1,'^3')+2); LINE @1 'cust:' cust $9. @25 'NAME:' NAME $22. @40 'SEX:' PAT_SEX $1.; ENDCOMP; COMPUTE BEFORE KEY2; proc=substr(key2,index(key2,'0'); LINE @1 'proc:' proc $12.; ENDCOMP; KEY3=CAT(charge,'^2',hrp_charge,'^3',rent_charge); COMPUTE BEFORE KEY3; charge=Substr(key3,1,index(key3,'^2')-1); hrp_charge=Substr(key3,index(key3,'^2')+2,(index(key3,'^3')-index(key3,'^2'))-2); rent_charge=Substr(key3,index(key3,'^3')+2,(index(key3,'^4')-index(key3,'^3'))-2); LINE @1 'charge:' charge $9. @22 'hrp_charge:' hrp_charge $9. @39 'rent_charge' rent_charge $9. ; ENDCOMP; COMPUTE BEFORE KEY4; cust=Substr(key4,1,index(key4,'^2')-1); CHARGES=Substr(key4,index(key4,'^2')+2,(index(key4,'^3')-index(key4,'^2'))-2); HRP_ALLOW=Substr(key4,index(key4,'^3')+2,(index(key4,'^4')-index(key4,'^3'))-2); PPOALLOW=Substr(key4,index(key4,'^4')+2); LINE @1 'TOTALS FOR :' cust $5.; LINE @1 'CHARGES:' CHARGES $7. @20 'HRP_ALLOW:' HRP_ALLOW $7. @40 'PPO_ALLOW:' PPOALLOW $8.; ENDCOMP; COMPUTE AFTER; LINE @1 "GRAND TOTALS" @15 "CHARGES: &CHARGES2" @40 "HRP_ALLOW: &HRP_ALLOW2" @65 "PPO_ALLOW: &PPOALLOW2";

ENDCOMP; run; ods pdf close;

I want report like below and must in the of PDF file. Pls send code very soon. cust:100 name:raja sex:M proc:890 charge:900 hrp_charge:1000 rent_charge:2000 cust:100 name:rani sex:F proc:300 charge:908 hrp_charge:123 rent_charge:8908 Total for 100 charge:1808 hrp_charge:1123 rent_charge:110908 cust:200 name:kaja sex:M proc:900 charge:987 hrp_charge:2000 rent_charge:8000 Total for 200 charge:987 hrp_charge:2000 rent_charge:8000 cust:300 name:hani sex:F proc:800 charge:908 hrp_charge:123 rent_charge:8908 Total for300 charge:908 hrp_charge:123 rent_charge:8908 Grand total charge:3595 hrp_charge:3723 rent_charge:19784 Sending email FILENAME myemail EMAIL from=("analystprasad@yahoo.co.in") to=("prasad7uk@gmail.com" ) cc=("prasad7bob@gmail.com") Subject = "An automatic email sent from SAS" Attach = "C:\Users\home\Desktop\New folder (3)\uk.pdf"; data _null_; file myemail; put "Your report is now available online." / / "Thank you and have a great day." / / " " / /"Sincerely," / /"Venkat Prasad Sandu" / / " " / /

"This is an automated email sent by SAS on behalf of Venkat Prasad Sandu"; run; Proc report data test; input Region $ Sales AgentID; cards; E 12 1 E 14 1 E 17 2 E 12 1 E 14 3 E 12 2 E 18 3 N 18 4 N 16 4 N 12 5 N 17 4 N 25 4 S 12 7 S 12 8 S 13 7 S 12 8 W 27 9 ; run; options nodate nonumber; title; ods pdf file ='C:\Users\home\Desktop\New folder (3)\rep.pdf'; proc report data = test nowd style=[frame=void rules=none ] style(header)=[background=white]; column Region Sales AgentID; define region / group; define sales / analysis sum; define AgentID / analysis n "Number of Agents"; rbreak after / summarize Ol UL; run; ods pdf close; Task 2: %let name=prasad; output: 'prasad' ( Need Prasad in inverted commas). Solution: %let name=prasad; %let ab=%str(%'&name%'); %put &ab; Task 3:

data trans; input CustomerID $ transactiondate mmddyy10. amount category $; cards; 9801234 10/01/1998 123.98 toys 9802234 12/10/1997 80.34 books 9802234 12/20/1997 100.00 apparel 9805556 08/01/1996 22.90 toys 9805556 09/10/1996 25.50 apparel 9805556 10/11/1996 18.90 books 9801134 11/11/1999 12.11 toys ; run; 1. Total and average amount spent by category 2. Which category has the highest average purchase 3. What is the average number of categories that customers purchase 4. What is the average and total amount by customer 5. What is the average number of days between purchases (as of today). data trans; input CustomerID $ transactiondate mmddyy10. amount category $; cards; 9801234 10/01/1998 123.98 toys 9802234 12/10/1997 80.34 books 9802234 12/20/1997 100.00 apparel 9805556 08/01/1996 22.90 toys 9805556 09/10/1996 25.50 apparel 9805556 10/11/1996 18.90 books 9801134 11/11/1999 12.11 toys ; run; /*(1)Total and average amount spent by category*/ proc sql; create table one as select category, sum(amount) as Total, avg(amount) as Average from trans group by category order by category; quit; /*(2)Which category has the highest average purchase*/ proc sql outobs=1; create table two as select category, avg(amount) as Average from trans group by category

order by 2 desc; quit; /*(3)What is the average number of categories that customers purchase */ proc sql; create table three as select CustomerID,count(distinct category) as Avg_cat from trans group by CustomerID; quit; /*(4)What is the average and total amount by customer */ proc sql; create table four as select CustomerID,sum(amount) as Total, avg(amount) as Average from trans group by CustomerID order by CustomerID; quit; /*(5)What is the average number of days between purchases (as of today) */ proc sort data=trans; by CustomerID transactiondate; run; data findavg(drop=mult_trans daysinbet td1 td2 amount category transactiondate); set trans; by CustomerID transactiondate; retain td1 td2; if first.CustomerID then do; daysinbet=0; td1=transactiondate; mult_trans=0; avgdays=0; end; else do; td2=transactiondate; daysinbet=td2-td1; mult_trans+1; avgdays=daysinbet/mult_trans; end; if last.CustomerID then output;

run; proc print;run;

/*Read in the data*/ data COUNTY; input COUNTY_ID $ STATE_NAME $ COUNTY_NAME $; cards; 1 Texas Collin 2 Texas Dallas 3 Georgia DeKalb ; run; /*Read in the data*/ data AGE_DISTRIBUTION_DESC; length CATEGORY_DESCRIPTION $23; input CATEGORY_NAME $ 1-11 CATEGORY_DESCRIPTION &; cards; AGE_0_10 < 10 years AGE_10_20 Between 10 and 20 years AGE_20_40 Between 20 and 40 years AGE_40_PLUS > 40 years ; run; data AGE_DISTRIBUTION; input COUNTY_ID $ AGE_0_10 AGE_10_20 AGE_20_40 AGE_40_PLUS; cards; 1 100 20 40 60 2 10 10 40 50 3 45 100 56 67 ; run; /*Sort the data for next merge step*/ proc sort data=COUNTY; by county_id; run; proc sort data=AGE_DISTRIBUTION;by county_id; run; /*Transpose the data by County_id*/ proc transpose data=AGE_DISTRIBUTION out=trans (rename=(_name_=category_name col1=total_num)); by county_id; run; /*Merge the 2 datasets to get the County Info at one place*/ data countyinfo; merge county trans; by county_id; run; /*Sort the data for next merge step*/

proc sort data=countyinfo; by CATEGORY_NAME; run; proc sort data=AGE_DISTRIBUTION_DESC; by CATEGORY_NAME; run; /*Merge the 2 datasets to get the County Info at one place*/ data final; merge countyinfo AGE_DISTRIBUTION_DESC; by CATEGORY_NAME; run; proc sort data=final; by county_name; run; /*Macro to Create the Report in HTML and Excel Version */ %macro report(type); filename report "C:\Users\home\Desktop \html - Copy\results.&type"; ods listing close; ods html body=report; /* First Report */ title; proc report data=final nowd split='*' style(header)=[foreground=blue background=grey] /*style for the header*/ style(column)=[foreground=black background=white]; /*style for the Columns*/ columns county_name ("Age Distribution" CATEGORY_DESCRIPTION total_num) ; define county_name / group 'County*Name' ; define CATEGORY_DESCRIPTION / 'Category' style(header)=[foreground=red background=grey]; define total_num / analysis 'Total Number' style(header)=[foreground=red background=grey]; /*break after county_name/skip ;*/ run; /* Second Report */ proc tabulate data=final format=3. style=[ background=white foreground=black]; /*style for the data area*/ class county_name/style=[background=grey foreground=red]; /*style for the column name*/ class category_description /order=data style=[background=grey foreground=red]; /*style for the column name*/ var total_num; table category_description='Category' all*{style=[background =white font_style=italic foreground=black]},county_name='County'*total_num=' ' ; keylabel all='Total' sum=' '; keyword all/style=[font_weight=extra_light background=white foreground=black font_style=italic]; classlev category_description county_name/style=[background=grey foreground=black]; run; /* Third Report */ proc tabulate data=final format=3. style=[background=white foreground=black];

class state_name/descend style=[background=grey foreground=red]; class category_description/order=data style=[background=grey foreground=red]; var total_num; table category_description='Category' all*{style=[background =white font_style=italic foreground=black]},state_name='State'*total_num=' '; keylabel all='Total' sum=' '; classlev category_description state_name/style=[background=grey foreground=black]; keyword all/style=[font_weight=extra_light background=white foreground=black font_style=italic]; run; ods html close; ods listing; %mend report; %report(html); /*HTML Version*/ %report(xls); /*Excel Version*/

Das könnte Ihnen auch gefallen