SAS Advanced programming 정리- PROC SQL (5)

Hoya Jaeho Lee·2022년 4월 8일
0

SAS Advanced programming

목록 보기
5/17

Chapter 5 - Creating and Managing Tables Using PROC SQL

Inserting Rows of Data into a Table

row를 추가하는 방법을 총 3가지 방법을 소개하고 있는데... 하나씩 한번 살펴보기로 하자:)

Inserting Rows By Using the SET Clause

코드 설명:
핵심은 insert into ~ set
set을 이용한 row 추가 부분이라고 할 수 있음:)

Inserting Rows By Using the VALUES Clause

proc sql;
insert into work.discount (destination,
begindate,enddate,discount)
values ('ORD','05MAR2000'd,'15MAR2000'd,.25)
values ('YYZ','06MAR2000'd,'20MAR2000'd,.10);

select *
from work.discount;

코드 설명:
insert into ~(col1, col2... coln)
values ('~','~','~',n1)
values ('~','~','~',n2)
이런 형식!!

Inserting Rows from a Query Result

-> 이미 존재하는 기존의 데이터 셋을 만들고

->전의 데이터 셋에서 where절을 사용하여 조건 충족하는 row를 추가하는 형태

Displaying Integrity Constraints for a Table

proc sql;
describe table constraints work.discount4;

Updating Values in Existing Table Rows

proc sql;
update work.payrollmaster_new
set salary=salary*1.05
where jobcode like '__1';

코드 설명:
해당 열에만(where) sales column을 업데이트하는 방식
update~ set구문


또 다른 방법으로 update를 활용한 case when ~구문

proc sql;
update work.payrollmaster_new
set salary=salary*
case
when substr(jobcode,3,1)='1'
then 1.05
when substr(jobcode,3,1)='2'
then 1.10
when substr(jobcode,3,1)='3'
then 1.15
else 1.08
end;

코드 설명:

salary*를 사용하여 해당하면 그 값에 곱합

case when ~ end 형식 코드 잘 알아두기!!!

위에 코드 참조:)

Deleting Rows in a Table

proc sql;
delete from work.frequentflyers2
where pointsearned-pointsused <= 0;

조건에 만족하는 행 삭제됨:)

Altering Columns in a Table

Alter Table statement

Adding Columns to a Table
proc sql;
alter table work.payrollmaster4
add Bonus num format=comma10.2,
Level char(3);

Dropping Columns from a Table
proc sql;
alter table work.payrollmaster4
drop bonus, level;

Modifying Columns in a Table
proc sql;
alter table work.payrollmaster4
modify salary format=dollar11.2 label="Salary Amt";

변수에 포맷, 길이, 라벨등 바꾸고 싶을 때 alter table ~ modify 사용

아래처럼 한번에도 사용가능:)
proc sql;
alter table work.payrollmaster4
add Age num
modify dateofhire date format=mmddyy10.
drop dateofbirth, gender;

Dropping Entire table

모두 삭제하는 경우: drop table 사용
proc sql;
drop table work.payrollmaster4;

profile
Biostatistics researcher Github: https://github.com/hoyajhl

0개의 댓글