"D:/EKCELON NEW/CENTER MATERIAL/Documents/mysaswork": / Opening A Library
"D:/EKCELON NEW/CENTER MATERIAL/Documents/mysaswork": / Opening A Library
airline; run; /*to view dataset*/ proc print data = EKC.hospital; run;
/*to view content variable order*/ proc contents data = EKC.airline varnum; run;
/*to view all content variable in AKC library*/ proc contents data = EKC._all_; run; /*to view all content variable in AKC librarywithout details*/ proc contents data = EKC._all_ nods; run;
/* print data without observation coloumn*/ proc print data = ekc.airline noobs; run;
/* print data without observation coloumn, show few var in the order we keep them*/ proc print data = ekc.airline noobs; var cust_id firstname lastname age; run;
/* print data with observation coloumn, show few var with obsservations*/ proc print data = ekc.airline; var cust_id firstname lastname age; run;
/* print data without observation coloumn, show few var & condition*/ proc print data = ekc.airline noobs; var cust_id firstname lastname age; where age > 30 ; run;
/* to sort data by city & view it */ proc sort data = ekc.airline; by city; run; proc print data = ekc.airline; run;
/* to sort data by city in decending & view it */ proc sort data = ekc.airline; by decending city; run; proc print data = ekc.airline; run;
proc sort data = ekc.airline out = ekc.airline_sort; by decending city; run; proc print data = ekc.airline; run;
/* Nodupkey - to extract only the first obs of thet record In this case all city names will be displayed but wont be repeated even if it is different data*/ proc sort data = ekc.airline nodupkey out = ekc.airline_sort; by decending city; run; proc print data = ekc.airline_sort; run;
/* Nodup - checks for & eliminates duplicate observation In this case all city names will be displayed with all different data*/ proc sort data = ekc.airline out = ekc.airline_sort nodup; by decending city; run; proc print data = ekc.airline_sort; run;
/* the subtotals of basefarewill be done in acsending order by default since in last command we sorted data in decending order we need to change it in acsending*/ proc sort data = ekc.airline out = ekc.airline_sort nodup; by city; run; proc print data = ekc.airline_sort; run;
/* get subtotals of basefare by city*/ proc print data = ekc.airline_sort; var cust_id lastname age basefare; sum basefare; by city; run;
/* Adding title & footnote*/ proc print data = ekc.airline_sort; title1 Airline charges in march; Footnote Charges sorted by city; run;
/*TO IMPORT TEXT FILE MANNUALLY*/ Filename extra 'D:\EKCELON NEW\CENTER MATERIAL\Documents\mysaswork\extra.txt'; /* extra is the name of the file */ data ekc.extra; /*to create file extra in Ekc */ infile extra; /*to bring file exta in sas lib*/ input ID $1-2 Firstname $4-11 lastname $13-19; /* to define the variable of the dataset imported , $ is used to signify Characters, also $ is used for ID becoz we wanna make it character and dont want anny mathematical operations on it */ run;
/* to create a new data file*/ data ekc.airline_new; /* to create a new data file with keeping selected variables */ data ekc.airline_new (keep= firstname lastname); /* to create a new data file with droping selected variables */ data ekc.airline_new (drop= firstname lastname); /* to create a new data file with renaming selected variables */ data ekc.airline_new (rename = dob= date of birth);
filename extra"D:\EKCELON NEW\CENTER MATERIAL\Documents\mysaswork\extra.txt"; data ekc.extra; /* to create a new file*/ infile extra truncover; /* source file*/ input ID 1-2 first name $ 4-11 lastname $ 13-19; /* to import wat content */ run; proc print; run;
/* to create a new file directly */ data ekc.extra2; input ID 1-2 firstname $ 4-11 lastname $ 13-19; cards; 12345678901234560 14 Amit Sati 11 Priyanka Bhatia 12 Naveen Narayan 13 Tanveer Ahmed ; run; /*to import text file use cards/ infile csv files use imports wizard/ proc import*/ data ekc.new_raw_data; input Soc 1-11 name $ 13-20 Fname $ 22-31 gender $ 33-38 tittle $ 41-49 salary $ 51-59 cat $ 62-71; cards; 12345678901234567890123456789012345678901234567890123456789012345678901 Soc_Sec_Num Name First name Gender Title Salary Category , 555 555 555 Jenkins David M Manager 27,000 $ 3, 222 222 222 Williams Jessica F Sales Rep 22,500 $ 2, 666 666 666 Smith Alex M Sales Rep 18,000 $ 1, 777 777 777 Crosby Julian M Manager 27,000 $ 3, 888 888 888 Albright Jackeline F Secretary 27,000 $ 3,
111 111 111 Sawyer John M Sales Rep 31,500 $ 4, 444 444 444 Thomas Martin M Secretary 22,500 $ 2, 999 999 999 Albright Benjamin M Worker 22,500 $ 2, 333 333 333 Crawford Marck M Manager 40,500 $ 4, 000 000 000 Perry Karl M Worker 37,100 $ 4, 123 456 789 Timmons Alice F Secretary 22,500 $ 2, 123 123 123 Thomas Rita F Manager 27,000 $ 3, 456 456 456 Carter Paul M Worker 20,000 $ 2, 789 789 789 Jenkins George M Manager 32,000 $ 4, 987 654 321 Williams Carol F Sales Rep 22,900 $ 2 ; run; /*another method to import file*/ proc import datafile ="D:\EKCELON NEW\CENTER MATERIAL\Documents\mysaswork\airline.csv" out= ekc.airline2 dbms = csv; run; data ekc.employee; input Sno 1 firstname $ 5-9 lastname $ 13-19 job_title $ 21-23 salary 25-28 promotion $ 33; cards; 1 Donny Evans 112 2325 Y 2 Lisa Helms 105 4558 N 3 John Higgins 105 4680 U 4 Mary Moore 111 3468 P ; run;
/* FORMAT it has a . in the end Eg: dollar.* Format is of two types permanent: Eg: data step temporary: eg: proc print to format any number into dollar eg:1234 $1,1234.00 Dollar9.2 */
data ekc.airport; set ekc.airline2; format basefare dollar9.2; /*since the largest variable value of base fare was 7 digit Eg 7932.15 it will become $ 7,932.15, now variable value is 9*/ format dob ddmmyy8.; run; /*create a format*/ proc format ; value $jobfmt /* any file name */ 105 = 'Assistant manager' 111 = 'senior analyst ' 112 = 'technical writer' ; run; proc print data = ekc.employee; format job_title $jobfmt. ; run;
proc format ; value $promotion 'Y'= 'Yes' 'N' = 'no ' 'U' = 'Undecided' 'P' = 'Promoted' ; run;
/*extracting the specific info from data set using IF*/ data ekc.insurance1; set ekc.insurance; if policy= 'child plan'; run;
data ekc.insurance2; set ekc.insurance; length policy_id $ 10.;/*to give lenth to output, otherwise by default it will take length=1*/ if policy= 'child plan' then policy_id='C'; else if policy= 'Mutual Funds' then policy_id='M'; else if policy= 'Pension Plan' then policy_id='P'; else policy_id= 'Not required'; run; proc print data = ekc.insurance2; run; data ekc.airline3; set ekc.airline2; length policy_id $ 10.; if pass_class= 'business' then Preference = 'HIGH'; else if pass_class= 'economy' then Preference = 'LOW'; else if pass_class= 'first' then Preference = 'BEST'; else Preference = 'NA'; run; /* if then DELETE */ data ekc.city_new; set ekc.airline2; if city in('bangalore','chennai', 'delhi')/*in is used to remove the repeatation of = */ then delete; run; /* select variable and wen statement */ data ekc.airline4 (keep= city destination); set ekc.airline; length destination $ 10.; select (city); when ('bangalore' , 'gurgaon') destination ='BG'; when ('chennai' , 'mysore') destination = 'CM'; when ('mumbai') destination = 'M'; otherwise destination= "Not req"; end; run; /*PROC OPTIONS
to list all options & settings*/ proc options; run; /* to list the value of one option*/ proc options option = yearcutoff; run; options yearcutoff = 2011; run; proc options option = yearcutoff; run; /*obs & first obs gives observations b/w specified range & becomes default untill we change it*/ proc print data = ekc.airline(firstobs= 10 obs = 20); run; proc print data = ekc.airline(firstobs= 10 obs = 20); run;
option first obs = 1 obs= 20; proc print data = ekc.airline; run; /*if we define it like as above it becomes global statement for all statement untill we change it */
/*data function page 361 SAS prep guide*/ /*all functions today using null & put function */ data _null_; set ekc.airline; length policy_id $ 10.; if pass_class= 'business' then Preference = 'HIGH'; else if pass_class= 'economy' then Preference = 'LOW'; else if pass_class= 'first' then Preference = 'BEST'; else Preference = 'NA'; put preference =; run;
/* Assigning data constant to variables */ data _null_; Testdate = '7nov1986'd; format testdate DDMMYY10.; put testdate=; run; /*create a date*/ data _null_; date = mdy (11,07,2010); format date DDMMYY10.; put date=; run; /*to see number of days*/ data _null_; date = mdy (11,07,2010); put date=; run; /*Eg: day =day ('17nov2010'd')= 17 */ data _null_; day= day('7nov1986'd); week= weekday('7nov1986'd); month=month ('7nov1986'd); quarter= qtr('7nov1986'd); year= year('7nov1986'd); put day=; put week=; put month=; put quarter=; put year=; run;
/* find interval check b/w 2 dates*/ data _null_; A = intck ('day', '25sep77'd ,'25sep78'd); B = intck ('weekday', '25sep77'd ,'25sep78'd); C= intck ('month', '25sep77'd ,'25sep78'd); put A =;
put B =; put C =; run; data _null_; A = intck ('day', '31dec2010'd , '01jan2012'd); B = intck ('weekday', '31dec2010'd , '01jan2012'd); C= intck ('month', '31dec2010'd , '01jan2012'd); put A =; put B =; put C =; run; /*case function- upcase, lowcase& propcase*/ data _null_; set ekc.builder; city=upcase(city); project =lowcase(project); facility = propcase (facility); put city=; put project=; put facility =; run;
data temp; input names $ 1-17 phone 19-25; cards; presley lisa mary 6224549 ; run; proc print; run;
/*Scan function*/ /* extracts the word using delimeters*/ data temp_new(drop= names); set temp; length firstname middlename lastname $12;
lastname = scan(names,1); firstname = scan(names,2); middlename =scan (names,3); run; proc print; run;
data temp_new(drop= names); set temp; length firstname middlename lastname $12; lastname = scan(names,1); firstname = scan(names,2); middlename =scan (names,3); middle_initial = substr (middlename, 1,1);(/* to create a new coloumn with only initial of middle name*/) run; proc print; run; /*concatenating or joining 2 variables and putting another variable*/ data temp_new; set temp_new; length firstname middlename lastname $12; Newname = firstname ||' '||middle_initial; /* || is used like & */ /* alternatively we can use this command for joining also Newname = catx(",",firstname,middle_initial; */ run; proc print; run; IIIIIIIIIIIIIIIIIIIIIIIII
libname ekc 'D:\EKCELON NEW\CENTER MATERIAL\Documents\mysaswork'; run; /*DO LOOPS starts with a do & ends with a end*/ /*It Contains WHILE(before) & UNTIL(after)*/ /*n=7; (starting point) do while(n<=700) (ending point); n+7; end;*/
/*eg: n=0 while n<5 until n=5 end; both the conditions will hav same output 1,2,3,4*/ /*GENERATING THE DATA WITH DO LOOPS CHAPTER 15 data _null_; do i =1to 5; put i= ; end; run; /*iterative DO LOOP- uses the BY increament statement*/ data _null_; do i = 1 to 10 by 2; put i= ; end; run; data _null_; do i = 10 to 1 by 3; put i= ; end; run; /*Interactive DO LOOP - uses WHILE & UNTIL DO WHILE executes the statement in a DO LOOP repeatively while a condition is True*/ /*Check the condition of the DO LOOP */ data _null_; n=0;do while (n<5); put n=; n+1; end; run; /*45,41,37......10*/ PAGE 402*/
data _null_; n=45; do while (n>10); put n=; n=n-4; end; run; /*97, 95, 93....35*/ data _null_; n=97; do while (n>35); put n=; n=n-2; end; run; /*45,41,37......10 USING UNTIL*/ data _null_; n=45; do until (n<=10); put n=; n=n-4; end; run; /*do j and do i*/ data _null_; do i=1 to 5; do j= 1 to 5; put i=j=; end; end; run;
do j= 1 to 10; if j=5 then continue;/* this will eliminate j= 5 but will continue with next step j=6*/ put i=j=; end; end; put 'this statement executive next'; run; data _null_; do i=1 to 10; do j= 1 to 10; if j=5 then leave;/* this will eliminate j= 5 put i=j=; end; end; put 'this statement executive next'; run; /*FIBONACCI SERIES*/ /*1 1 2 3 5 8 13 21 34 55 */ data _null_; a=0; b=1; do i=1 to 10;/*i is used to define c=a+b; put c=;/*throw result*/ b=a; a=c; end; run;
LOOP */