Sie sind auf Seite 1von 17

/*Chapter 11 & 12: Creating and ManagingVariables/Reading SAS Data Sets*/

*-----------------------------------------------------;

/*Label*/

*-----------------------------------------------------;

Data Class;

set sashelp.class;

run;

Data Class_label;

set class;

Label Age = 'Five years old employee age';

Current_age = Age + 5;

run;

Proc print data = Class_label label;

run;

*-----------------------------------------------------;

/*Rename = change the name of the column*/

*-----------------------------------------------------;

/*Rename <old col name> = <new col name>*/

Data Class1;

set sashelp.class;

Rename Age = Age_Student;

run;
*-----------------------------------------------------;

/*Keep: filter the column in DATA statments*/

*-----------------------------------------------------;

Data Shoes;

Set sashelp.shoes;

run;

data shoes1;

set shoes(keep = Region Sales);

run;

PROC PRINT DATA = SHOES1;

RUN;

*-----------------------------------------------------;

data shoes1(keep = Region Sales);

set sashelp.shoes;

run;

PROC PRINT DATA = SHOES1;

RUN;

*-----------------------------------------------------;

data shoes1(keep = Region Bonus );

set sashelp.shoes;

Bonus = Sales * .10;

run;

*-----------------------------------------------------;

/*Drop = delete */

*-----------------------------------------------------;
data shoes1;

set shoes(Drop = Region Sales);

run;

PROC PRINT DATA = SHOES1;

RUN;

*-----------------------------------------------------;

data shoes1(Drop = Region Sales);

set sashelp.shoes;

run;

*-----------------------------------------------------;

/*Firstobs and obs*/

*-----------------------------------------------------;

/*Firstobs = lower limit , OBS = upper limit*/

data classobs;

set sashelp.class(firstobs=5);

run;

proc print data = classobs ;

run;

*-----------------------------------------------------;

data classobs2;
set sashelp.class(obs=10);

run;

proc print data = classobs2;

run;

*-----------------------------------------------------;

data classobs3;

set sashelp.class(firstobs=2 obs=5);

run;

proc print data = classobs3;

run;

*-----------------------------------------------------;

/*problem*/

*-----------------------------------------------------;

/*obs 1 to 65500*/

Data Inventory;

set inv (obs = 65500);

run;

Data Inventory1;

set inv (Firstobs = 65501 obs = 131000);

run;

Data Inventory2;
set inv (Firstobs = 131001 obs = 196500);

run;

Data Inventory3;

set inv (Firstobs = 196501 );

run;

proc export data = Inventory1

outfile = 'D:\abc.xls';

DBMS = excel;

Sheet = 'First';

run;

proc export data = Inventory2

outfile = 'D:\abc.xls';

DBMS = excel;

Sheet = 'second';

run;

*-----------------------------------------------------;

/*Assignment*/

*-----------------------------------------------------;

/*How to send 3,00000 data in excel file.*/

*=============================================;

/*Point

output
stop*/

data random;

A = 17;

set sashelp.class point = A;

output;

stop;

run;

data random1;

Do a = 2 to 19 by 2;

set sashelp.class point = a;

output;

End;

stop;

run;

*=============================================;

/*IF Conditions*/

*------------------------------------------------;

/*IF*/

*------------------------------------------------;

Data new;

set sashelp.class;

If age > 12;

run;
proc print data = new;

run;

*------------------------------------------------;

Data New1;

set sashelp.class;

IF Name = 'Henry';

run;

proc print data = New1;

run;

*------------------------------------------------;

Data New1;

set sashelp.class;

IF Name IN ('Henry','Judy');

run;

proc print data = New1;

run;

*------------------------------------------------;

/*IF then delete*/

*------------------------------------------------;

Data new3;

set sashelp.class;

If age > 12 then delete;

run;
proc print data = New3;

run;

*------------------------------------------------;

/*Logical oprator*/

*------------------------------------------------;

1. And

2. OR

3. Not

*------------------------------------------------;

/*And*/

*------------------------------------------------;

Data new2;

set sashelp.class;

If age = 12 And Sex = 'F';

run;

proc print data = new2;

run;

*------------------------------------------------;

/*OR*/

*------------------------------------------------;

Data new4;

set sashelp.class;

If age = 12 or Sex = 'F';


run;

proc print data = new4;

run;

*------------------------------------------------;

/*NE = NOT Equal to*/

*------------------------------------------------;

Data new5;

set sashelp.class;

If age NE 12;

run;

proc print data = new5;

run;

*------------------------------------------------;

/*If then else*/

*------------------------------------------------;

*IF <Condition> Then <New col_name> = <Target_val)

Else if <Condition> Then <New col_name> = <Target_val)

Else <New col_name> = <Target_val) ;

data emps ;

length Group $ 25.;

set sashelp.class;

IF Age = 11 then group="Flight Attendant I";


Else if Age = 12 then group="Flight Attendant II";

Else if Age = 13 then group="Flight Attendant III";

Else if Age = 14 then group="Mechanic I";

Else if Age = 15 then group="Mechanic II";

Else group="Other";

run;

proc sort data = Emps;by age;run;

Proc print data = emps;run;

*------------------------------------------------;

/*Select When otherwise End*/

*------------------------------------------------;

data emps1 (keep = age group);

length Group $ 25.;

set sashelp.class;

select (Age);

when (11) group="Flight Attendant I";

when (12) group="Flight Attendant II";

when (13) group="Flight Attendant III";

when (14) group="Mechanic I";

when (15) group="Mechanic II";

otherwise group="Other";

end;

run;

proc sort data = Emps1;by age;run;


Proc print data = emps1;run;

*------------------------------------------------;

/*Proc copy*/

*------------------------------------------------;

/*In = source library*/

/*Out = Des library*/

proc copy in = source_library out = target_library;

run;

*------------------------------------------------;

libname a 'D:\Students Folder\';

Proc copy in=work out=A;

run;

Proc copy in=Sashelp out=work;

Select Class Air buy Shoes;

run;

*------------------------------------------------;

/* Change the SAS dataset name */

*------------------------------------------------;

PROC DATASETS LIBRARY = work;

CHANGE class = student;

QUIT;

*------------------------------------------------;
/*Delete the SAS datasets*/

*------------------------------------------------;

proc datasets library=work;

delete Buy;

Quit;

*------------------------------------------------;

/*Delete all the SAS dataset*/

*------------------------------------------------;

proc datasets library=work kill;

quit;

*------------------------------------------------;

/*Accumulation*/

*------------------------------------------------;

data year2001;

input year qty rev;

cards;

2001 1 2000

2001 2 4000

2001 3 8000

2001 4 12000

run;

/*tol_rev new column*/


data accu_2001;

set year2001;

tol_rev + rev;

run;

/**/

/*tol_rev + rev = tolrev*/

/* 0 2000 2000*/

/* 2000 4000 6000*/

/* 6000 8000 14000*/

/* 14000 12000 26000*/

/**/

*------------------------------------------------;

/*End*/

*------------------------------------------------;

data end1;

set accu_2001 end = abc;

if abc;

run;

*------------------------------------------------;

data year2002;

input year qty rev;

cards;

2002 1 2000

2002 2 4000

2002 3 8000
2002 4 12000

run;

*------------------------------------------------;

/*Retain */

*------------------------------------------------;

data accu_2002;

set year2002;

retain T_rev 26000;

T_rev + rev;

run;

/*tol_rev + rev = tolrev*/

/* 26000 2000 28000*/

/* 28000 4000 32000*/

/* 32000 8000 40000*/

/* 40000 12000 52000*/

/**/

*------------------------------------------------;

/*First.var and last.var syntax*/

*------------------------------------------------;

data temp;

input group salary;

cards;

1 23

1 34
1 .

1 45

2 78

2 92

2 45

2 89

2 34

2 76

3 31

4 23

4 12

run;

/**************************************************

The automatic variables first.group and last.group are not saved with the data set.

Here we write them to data set variables to show their contents.

**************************************************/

data new;

set temp;

by group;

first=first.group;

last=last.group;

run;

proc print data = new;

title 'Raw data along with first.group and last.group';

run;
*------------------------------------------------;

/*Add the salary by group*/

*------------------------------------------------;

data cusum(keep=group sum);

set temp;

by group;

if first.group then sum=0;

sum+salary;

if last.group then output;

run;

proc print data=cusum noobs;

title 'Sum of X within each group';

run;

*------------------------------------------------;

data temp;

input LOC $ part $ Qty;

cards;

A XX 20

A XX 40

A XX 80

B YY 100

C CC 80

C CC 30

C YY 10

;
run;

Data A;

SET Temp;

Key = LOC || PART;

Run;

Proc sort data = A; by Key; run;

Data A1;

Set A;

By Key;

If first.Key then do;

Put1 = 0 ; ;

end;

Put1 + Qty ;

If last.Key then output;

Run;

Data A2(drop = Key QTY);

set A1;

Rename PUT1 = T_Qty;

run;

Das könnte Ihnen auch gefallen