BookmarkSubscribeRSS Feed

PROC SQL 활용예시 1

Started 2 weeks ago by
Modified 2 weeks ago by
Views 156

 

 SAS는 전통적으로 DATA Step과 PROC 기반의 언어이지만, PROC SQL문을 활용하면 SQL을 사용하여 데이터 추출, 결합, 요약 작업을 단 하나의 단계로 간결하게 처리할 수 있습니다.

가상의 인사 데이터(employee_sample) 를 사용해 SAS에서 SQL을 어떻게 활용할 수 있는지 실전 예제를 통해 확인하겠습니다.

아래 코드는 사원ID, 성명, 근속연수, 부서, 직급, 연봉, 성과등급, 근무지역, 입사유형 등 총 9개 항목으로 구성된 15명의 가상 데이터를 생성하는 예시입니다.

 

 

data employee_sample;
    length person_id 8
           name $10
           department $12
           position $10
           performance $1
           location $10
           hire_type $8;
           
    input person_id
          name $
          tenure
          department $
          position $
          salary
          performance $
          location $
          hire_type $;
          
datalines;
1001 Kim   1  HR        Staff      4200 B Seoul   Regular
1002 Lee   3  Finance   Senior     5200 A Seoul   Regular
1003 Park  5  IT        Senior     6000 A Busan   Regular
1004 Choi  2  Marketing Staff      4100 C Seoul   Contract
1005 Jung  7  IT        Manager    7500 A Seoul   Regular
1006 Kang  4  Finance   Staff      4800 B Incheon Regular
1007 Yoon 10  HR        Manager    7800 A Seoul   Regular
1008 Jang  6  Sales     Senior     6200 B Daegu   Regular
1009 Lim   1  Sales     Staff      4000 C Seoul   Intern
1010 Han   8  IT        Manager    8000 A Busan   Regular
1011 Shin  3  Marketing Senior     5300 B Seoul   Regular
1012 Oh    9  Finance   Manager    8200 A Seoul   Regular
1013 Seo   2  HR        Staff      4300 B Incheon Contract
1014 Kwon 12  Sales     Director   9500 A Seoul   Regular
1015 Baek  5  IT        Senior     6100 B Daejeon Regular
;
run;

 

 

 

 

 

 

개인별로 속한 부서의 평균 연봉보다 더 많이 받는 사원 추축

 

기존의 Data Step에서는 1)부서별로 평균을 구하고(PROC MEANS), 2)값을 받아 원본 데이터와 합친 뒤(MERGE), 3) 비교(IF)하는 3단계 과정을 거쳐야 합니다.

PROC SQL을 활용하면 SAS의 SubQuery 를 활용해 데이터를 필터링해 한번의 쿼리로 해결할 수 있습니다.

 

proc sql;
    select a.name, a.department, a.salary
    from employee_sample as a
    where a.salary >
          (select avg(b.salary)
           from employee_sample as b
           where a.department = b.department);
quit;

 

SELECT문을 활용해 전체 사원 리스트를 확인하고,

SubQuery 로 부서의 평균 연봉을 계산합니다.

where 조건문으로 실제 연봉과 계산된 평균을 비교합니다.

 
 
image.png

 

 
 
 
 

근속·연봉 기반 직원 등급 분류

 

CASE - WHEN 문을 활용하여 근속연수(tenure)와 연봉(salary)에 따라 세 그룹으로 분류한 예시입니다.

  • 근속연수 8년이상 & 연봉이 7500 이상 - Core 그룹

  • 근속연수가 5년 이상 - Experienced

  • 이외 - Junior

다중처리 조건일 때는 IF - THEN - ELSE 보다는 문법 구조가 가독성이 좋으며 쿼리만 보고도 로직을 파악하기 쉽습니다. 대신, CASE 문은 END 문을 활용해 조건문을 닫아줘야합니다.

 
 
proc sql;
    select name,
           tenure,
           salary,
           case
               when tenure >= 8 and salary >= 7500 then 'Core'
               when tenure >= 5 then 'Experienced'
               else 'Junior'
           end as emp_level
    from employee_sample;
quit;
 
image.png

 

 
Contributors
Version history
Last update:
2 weeks ago
Updated by:

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

Register now

Article Labels
Article Tags