이번 게시글은 문자형 변수(Character Variables) 1의 이어지는 글 입니다.
proc import out = sales_data
datafile= "/home/u45061472/sales_data.xlsx"
dbms=xlsx replace;
getnames=yes;
run;
proc contents data= sales_data; run;
data test1;
set sales_data;
type = 'default';
run;
data mark_discount;
set test1;
if discount = 0 then type ='without discount';
else type = 'discounted';
run;
mark_discount 데이터셋을 보면 type 변수에 'discoun', 'without'까지만 텍스트가 보입니다.
SAS에서는 새로운 문자형 변수를 data 단계에서 생성할 때, 길이를 명시하지 않으면 처음 할당되는 값의 길이에 따라 자동으로 변수의 길이가 설정됩니다.
test1 데이터셋에서 type 변수는 'default'라는 7자짜리 문자열로 처음 생성되었기 때문에, SAS에서는 type 변수를 길이 7자의 문자형 변수로 고정합니다.
그래서 test1데이터 셋 이후로 만들어진 mark_discount 데이터셋에서도 type 변수는 동일하게 길이 7자로 유지됩니다.
data test1;
length type $20;
set sales_data;
type = 'default';
run;
data mark_discount;
set test1;
if discount = 0 then type ='without discount';
else type = 'discounted';
run;
처음 test1에 type이라는 변수를 생성할 때, Length 문을 사용해 변수의 길이를 명시하면 (최대 20자까지 저장 가능) 위 결과와 같이 문자열이 잘리지 않고 저장됩니다.