BookmarkSubscribeRSS Feed

SAS Macro - ③ %STR

Started ‎11-02-2025 by
Modified ‎11-02-2025 by
Views 112

%STR은 SAS 매크로로 특수문자나 연산자 등을 그대로 인식할 수 있는 매크로 함수입니다.

매크로 컴파일러가 +, -, , ,(쉼표), (, ) 등의 기호를 문자 그대로 저장합니다.

다양한 수식과 복수의 계산식을 하나의 매크로 변수로 묶어서 사용할 수 있습니다.

또한, 계산식을 한곳에서만 관리해서 코드가 수정이 필요할 때는 수정이 용이합니다. (유지보수 용이)

하나의 코드로 여러 데이터를 처리할 수 있습니다.

타입별로 계산식이 자동으로 적용하여 새로운 데이터 타입을 추가해 실무에서 바로 쓸 수 있는 템플릿을 작성할 수 있습니다.

 

 

■ 조건에 따른 수식 적용

 

%MACRO dynamic_calculate(dataset, calc_type);
    
    %IF &calc_type = car %THEN %DO;
        %PUT NOTE: 자동차 효율성 계산 적용;
        
        DATA work.result_&calc_type;
            SET &dataset;    
            efficiency = MPG_City / (Weight / 1000);
            power_ratio = Horsepower / Cylinders;
        RUN;
    %END;
    %ELSE %IF &calc_type = student %THEN %DO;
        %PUT NOTE: 학생 건강 지표 계산 적용;
 
        DATA work.result_&calc_type;
            SET &dataset;
            bmi = Weight / ((Height / 100) ** 2);
            age_group = FLOOR(Age / 5) * 5;
        RUN;
    %END;
    %ELSE %DO;
        %PUT ERROR: 지원하지 않는 계산 유형입니다 - &calc_type;
        %RETURN;
    %END;
    
    PROC PRINT DATA=work.result_&calc_type(OBS=5);
        TITLE "동적 계산 결과 - &calc_type 유형";
    RUN;
    
%MEND dynamic_calculate;

%dynamic_calculate(sashelp.cars, car);
%dynamic_calculate(sashelp.class, student);

 

 

1. 매크로 정의: dynamic_calculate 라는 매크로를 정의합니다.

2. 데이터 별로 수식 설정: 데이터 셋이 car (cal_type)일 경우 effiency (연비 효율성)과 power_ratio(마력)을 계산합니다.

학생데이터(cal_type = student)일 경우 bmi(체질량 지수)와 age_group(5세 단위 그룹 설정 ; ex) 10, 15,20 ) 칼럼을 생성합니다.

3. 데이터셋 생성: result_car 또는 result_student 테이블을 생성합니다.

4. 실행 결과: result_car과 result_student 라는 데이터샛을 결과로 보여줍니다.

 

 

image.png

 

 

 

 

 

 

 

 

 

 

Contributors
Version history
Last update:
‎11-02-2025 02:59 AM
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 lock in 2025 pricing—just $495!

Register now

Article Labels
Article Tags