%INDEX 는 문자열 내의 특정 부분의 위치를 찾는 함수입니다.
문자열이 처음 나타나는 위치가 출력됩니다.
%INDEX(검색할 문자, 찾고자 하는 문자)
'찾고자 하는 문자'가 발견되는 시작위치를 출력합니다.
검색 문자열이 시작되는 위치는 1부터 정수값을 출력하고, 문자열이 없으면 0을 출력합니다.
■ %INDEX - 문자열 포함되어 위치 출력
%LET 변수에 문자열을 저장합니다.
FIND 라는 매크로 변수에 "die"라는 문자열을 저장합니다.
찾는 문열이 없으면 0을 반환하고 문자열이 있으면 위치를 출력합니다.
%PUT문으로 로그에 메시지를 출력합니다.
%let sentence = Live as if you were to die tomorrow. ;
%let find = die;
%let position = %index(&sentence, &find);
%put result position: &position;
■ %INDEX - 문자열 포함되어 있지 않은 경우
%INDEX(string, substring) 구문에서 처음 등장하는 위치를 출력하지만 아래의 코드에는 sentence 구문에 find(=square) 값이 없기 때문에 위치를 반환하지 않고 0을 출력합니다.
%let sentence=%str(When you have faults, do not fear to abandon them);
%let find=square;
%let position=%index(&sentence,&find);
%put result position: &position;
■ %INDEX - 대소문자 구문
%INDEX 구문의 경우 대소문자를 구분합니다.
sentece 구문 안에 대문자와 소문자가 있으며 search 1에는 "MACRO" , search2 에는 "macro" 라는 단어가 있습니다.
"MACRO" 는 sentencr에 없기 때문에 pos1 = 0이 출력되고,
"macro"는 sentence에 있기 11번째 문자에 위치하고 있기 때문에 pos2 = 11 이 출력됩니다.
%let sentence = Hello SAS macro language!;
%let search1 = MACRO;
%let search2 = macro;
%let pos1 = %index(&sentence, &search1);
%let pos2 = %index(&sentence, &search2);
%put >> Search 1 (UPPERCASE): &pos1;
%put >> Search 2 (lowercase): &pos2;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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 lock in 2025 pricing—just $495!