BookmarkSubscribeRSS Feed

날짜 데이터 핸들링

Started 3 weeks ago by
Modified 3 weeks ago by
Views 239

데이터 분석을 하면, 날짜 처리, 형식 변환하는 방법이 필요합니다.

사용자의 거래 발생일 등 모든 비즈니스 데이터에는 시간 정보가 대부분 포함되어 있습니다.

이번 게시글에는 SAS에서 날짜 데이터를 효과적으로 다루는 과정에 대해 설명하겠습니다.

■ Date 계산

 

data test;
anniversary = '13June2011';
temp = anniversary - 1;
run;

 

temp에 잘못된 값을 출력합니다.

그 이유는 anniversary 변수는 문자열로 입력되었기 때문입니다.

SAS에서는 날짜를 계산하려면 numeric data value여야 합니다. (SAS는 자동으로 날짜형으로 인식하지 않습니다.)

 

 

 

data test;
today = today();
format today date9.;
yesterday = today - 1;
run;

proc print data = test; run;

 

image.png

 

 

위의 코드를 출력하면 today는 날짜 형식으로 지정해서(FORMAT) 날짜형식으로 출력이 되지만, yesterday의 경우 날짜 형식으로 지정하지 않아서 숫자로 출력됩니다.

 

 

 

 

data test;
today = today();
format today yesterday date9.;
yesterday = today - 1;
run;

proc print data = test; run;

 

image.png

 

 

 

 

data diff_date;
today = today();
anniversary = '12APR2022'D;
format today anniversary date9.;
diff_days = today - anniversary;
run;

 

image.png

 

 

 

위의 코드는 기간을 계산할 수 있는 예시입니다.

**문자형 뒤(12APR2022)에 D를 붙히면 SAS가 이 값을 날짜로 인식할 수 있습니다. 하지만, 내부적으로는 1960.01.01을 기준으로 며칠이 지났는지에 대한 숫자로 저장됩니다.

 

 

 

 

 

data test3;
birthday = '20APR2023'd;
format birthday date9.;
prepare  = intnx('month',birthday, -1);
format prepare date9.;
run;
proc print data = test3; run;

 

image.png

 

 

 

위의 코드는 특정 날짜로부터 날짜 간 이동을 수행하는 코드입니다.

먼저, birthday 라는 변수에 D를 붙혀 날짜로 인식할 수 있게합니다.

그 후, format 함수를 사용해 사람이 읽을 수 있는 형식인 Date9.를 적용합니다. (20APR2023)

INTNX() 는 Date Increment 를 수행하는 함수로 날짜 간 이동을 실행합니다.

그 결과, birthday 보다 한 달 앞선 달을 출력합니다.

 

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Article Labels
Article Tags