Tuesday, March 11, 2014

SAS- Miscellaneous Topics



SAS PROGRAMS FOR ABSOLUTE BEGINNERS BY AJEET KUMAR


/* Miscellaneous Program Number: 1 */
data dated ;
input dob :mmddyy10.;
format dob date9. ;
datalines;
1/1/1960
1/2/1960
1/3/1960
1/1/1959
11/17/2013
;
run;
proc print data =dated;
run;

/* Miscelleneous Program Number: 2 */
data date1 ;
informat dob mmddyy10. ;
input dob ;
format dob date9. ;
datalines;
01/01/1960
1/2/1960
1/3/1960
1/1/1959
11/17/2013
;
run;
proc print data =date1;
run;

/* Miscelleneous Program Number: 3 */
data date1 ;
input dob mmddyy10. ;
year=year(dob);
month=month(dob);
day=day(dob);
date= mdy(month, day, year);
format dob date9. date date9.;
DaysPassed= today()- dob;
DaysPassedSinceX= '15NOV2013'd - dob;
datalines;
01/01/1960
1/2/1980
1/3/1980
11/1/1959
4/17/1993
;
run;
proc print data =date1;
run;

/* Miscelleneous Program Number: 4 */
data prefix;
input sales_price sales_qty ;
sales_rev = sales_price * sales_qty ;
datalines;
12.4 23
13.5 32
;
run;
proc print data = prefix ;
var sales_: ;
run;

data list_num;
input x1 - x5 ;
datalines;
12.4 23 33 54 33
13.5 32 32 65 66
;
run;
proc print data = list_num ;
run;

proc contents data = sashelp.class varnum;
run;
data ddash;
set sashelp.class;
run;
proc print data = ddash ;
var sex -- weight ;
run;

proc print data = ddash ;
var _character_ ;
run;

proc print data = ddash ;
var _numeric_ ;
run;

proc print data = ddash ;
var _all_ ;
run;

/* Miscelleneous Program Number: 5 */
data miss;
missing xa ;
infile cards missover ;
input name $ age weight gender $;
cards;
amit 23 421 M
sumit 12 x M
vimla 44 a F
;
run;
proc print data = miss ;
run;

/* Miscelleneous Program Number: 6 */
options missing = xa ; /* global */
data miss;
infile cards missover ;
input name $ age weight gender $;
cards;
amit 23 421 M
sumit 12 x M
vimla 44 a F
;
run;
proc print data = miss ;
run;

/* Miscelleneous Program Number: 7 */
data missin;
missing xa ;
input name $ age weight gender $;
if weight = .a or weight = .x ;
cards;
amit 23 421 M
sumit 12 x M
vimla 44 a F
;
run;
proc print data = missin ;
run;

/* Miscelleneous Program Number: 8 */
data sorty;
missing xa ;
input name $ age weight gender $;
cards;
amit 23 4 M
sumit 12 x M
vimla 44 a F
Ravi 22 -2 M
Tanu 32 0 M
Kanta 12 . F
;
run;
proc sort data = sorty ;
by weight ;
run;
proc print data = sorty ;
run;

/* Miscelleneous Program Number: 9 */
data missin;
missing xa ;
input name $ age weight gender $;
if weight = . ;
cards;
amit 23 421 M
sumit 12 . M
vimla 44 . F
;
run;
proc print data = missin ;
run;

/* Miscelleneous Program Number: 10 */
data date1 ;
informat dob mmddyy10. ;
input dob ;
format dob ddmmyyb10. ;
datalines;
01/01/1960
1/2/1960
1/3/1960
1/1/1959
11/17/2013
;
run;
proc print data =date1;
run;

data date1 ;
informat dob mmddyy10. ;
input dob ;
format dob ddmmyyc10. ;
datalines;
01/01/1960
1/2/1960
1/3/1960
1/1/1959
11/17/2013
;
run;
proc print data =date1;
run;
data date1 ;
informat dob mmddyy10. ;
input dob ;
format dob ddmmyyd10. ;
datalines;
01/01/1960
1/2/1960
1/3/1960
1/1/1959
11/17/2013
;
run;
proc print data =date1;
run;
data date1 ;
informat dob mmddyy10. ;
input dob ;
format dob mmddyyp10. ;
datalines;
01/01/1960
1/2/1960
1/3/1960
1/1/1959
11/17/2013
;
run;
proc print data =date1;
run;
data date1 ;
informat dob mmddyy10. ;
input dob ;
format dob mmddyys10. ;
datalines;
01/01/1960
1/2/1960
1/3/1960
1/1/1959
11/17/2013
;
run;
proc print data =date1;
run;

/* Miscelleneous Program Number: 11 */
data operate;
input x y;
plus = x + y;
minus = x - y;
product = x * y;
division = x / y;
exponent = x ** y ;
band = x and y ;
bor = x or y ;
notx = not x ;
Less = x lt y ;
maximum = x <> y ;
minimum = x >< y ;
cards;
0 2
6 2
;
run;
proc print data = operate ;
run;


/* Miscelleneous Program Number: 12 */
data _null_;
infile "D:\abc1.txt";
file "D:\abc2.txt";
input;
put _infile_;
run;

/* Miscelleneous Program Number: 13 */
proc contents data = sashelp.class;
run;
proc datasets ;
contents data = sashelp._all_ nodetails;
quit;

proc datasets;
contents data=sasuser.admit varnum;
quit;
options nonumber nodate ps=88 ls=99 firstobs=2 obs=9 yearcutoff=1990;
proc print data=clinic.admit;
var id sex age height weight;
where age>=30;
run;
options date source leftmargin=11 user=sashelp errors=5 nofmterr;
proc freq data=clinic.diabetes;
where fastgluc>=300;
tables sex;
run;


/* Miscelleneous Program Number: 14 */
proc copy in=work out=good move ;
select ssss;
run;

/* Miscelleneous Program Number: 15 */
PROC EXPORT DATA= WORK.Class
            OUTFILE= "C:\Documents and Settings\class\My Documents\class.txt"
            DBMS=TAB REPLACE;
RUN;

/* Miscelleneous Program Number: 16 */
data ;
input x y @@ ;
file print;
put _all_;
put _PAGE_;
cards;
1 2 3 4 5 5
;
run;

/* Miscelleneous Program Number: 17 */
options ps =111 nocenter;
data miss;
input Age @@ ;
if Age = . then abort ;
put _all_ ;
datalines;
22 34 3 45 56 34 45 67 . . 44 5 . 44
;
run;
proc print data = miss ; run;


/* Miscelleneous Program Number: 18 */
data rare;
input X @;
if X= . then error "Error prints message in log";
input Name $ @@ ;
cards;
1 One 2 Amar 3 Danny 4 Rina
5 Five . Six 7 Seven . Eight
;
run;
proc print data=rare;
run;

/* Miscelleneous Program Number: 19 */
data class;
set sashelp.class;
/*if 10 < Age <= 15 then AgeGrp = 3 ;*/
/*if 15 < Age <= 20 then AgeGrp = 4 ;*/
AgeGrp = 2 + ( Age >= 10) + ( Age >= 15) ;
run;
proc print data = class ; run;


/* Miscelleneous Program Number: 20 */
options ps =111 nocenter;
data class;
set sashelp.class;
Category = ifc( Age > 13, 'Adolsc', 'Child');
PocketMoney = ifn( Age > 13, 111, 55);
run;
proc print data = class ; run;


/* Miscelleneous Program Number: 21 */
options ps =111 nocenter;
data miss;
input Age @@ ;
if not (Age = .) then output;
datalines;
22 . . 44 5 . 44
;
run;
proc print data = miss ; run;


/* Miscelleneous Program Number: 22 */
/*options _last_ = data1;*/
data miss;
infile datalines ;
input Gender $ Age @@ ;
if missing(Age) then delete;
/*if Age = . then delete ;*/
datalines;
M 33 F . M 11 M 31 M 12 M 22 F .
F 11 F 32 M 21 F 28 F 21 F 22 M .
;
run;
proc print data=miss; run;

/* Miscelleneous Program Number: 23 */
/*nobs implies number of obs*/
/*point option of set statement determines the obs to be read*/
data small;
do obsnum = 1 to total by 2;
set sashelp.class point=obsnum nobs=total;
if _error_ then abort;
output;
end;
stop;
run;
proc print data=small;run;
/*proc print data=sashelp.class;run;*/

/* Miscelleneous Program Number: 24 */
data sim;
do i=1 to 20;
group = int(5 * ranuni(54321)) + 1;
y = rannor(12345);
output;
end;
keep group y;
run;
proc print data=sim;
run;

/* Miscelleneous Program Number: 25 */
data rare;
input X @;
if X= . then return;
input Name $ @@ ;
cards;
1 One 2 Amar 3 Danny 4 Rina
5 Five . Six 7 Seven . Eight
;
run;
proc print data=rare;
run;

/* Miscelleneous Program Number: 26 */
options ps =111 nocenter;
data miss;
input Age @@ ;
if Age = . then stop ;
datalines;
22 34 3 45 56 34 45 67 . . 44 5 . 44
;
run;
proc print data = miss ; run;


/* Miscelleneous Program Number: 27 */
options validmemname=extend validvarname=any;
data  datachar ;
input ' var name 'n $;
datalines;
2
2
3
4
;
run;

/* Miscelleneous Program Number: 28 */
options validmemname=extend validvarname=any;
data  convar;
input ' var name 'n $ convar $;
convar = 'Rs.' || ' var name 'n ;

datalines;
2
2
3
4
;
run;

/* Miscelleneous Program Number: 29 */
data _null_;
window start
#9 @26 'WELCOME TO THE SAS SYSTEM' color=blue
#12 @19 'THIS PROGRAM CREATES'  color= red
#12 @40 'TWO SAS DATA SETS' color=brown
#14 @26 'AND USES THREE PROCEDURES'
#18 @27 'Press ENTER to continue';
display start;
stop; run;

/* Miscelleneous Program Number: 30 */
proc contents data= class;
run;
proc contents data= class varnum;
run;
proc contents data= class directory;
run;
proc print data = class;
run;
proc sort data = class ;
by name ;
run;

No comments:

Post a Comment

Hot Topics