BookmarkSubscribeRSS Feed

[SAS 프로그래밍 고수 백승민] [변수유형] 문자형 변수를 동일한 이름의 수치형 변수로 변경하기

Started ‎06-11-2020 by
Modified ‎06-11-2020 by
Views 81

* 출처 : http://support.sas.com/kb/40/700.html;
* Sample 40700: How to convert all character variables to numeric and use the same variable names
* in the output data set;

 

* 문자형 변수를 동일한 이름의 수치형 변수로 변경하기;
data test;
input id $ b c $ d e $ f;
datalines;
AAA 50 11 1 222 22
BBB 35 12 2 250 25
CCC 75 13 3 990 99
;

* varnum : 변수위치번호, name : 변수이름, type : 변수형태;
proc contents data=test out=vars(keep=varnum name type) noprint;
run;

 

* 문자형 변수의 형태를 수치형 변수로 변경 후 원래 변수 순서로 변경하기 위하여 변수 리스트 생성;
proc sql noprint;
select trim(left(name))
into :TOTAL_list separated by " "
from vars
order by varnum;
quit;

%PUT &TOTAL_list;

data vars;
set vars;
* 문자 변수 선택(TYPE=1 : 수치형 문자, TYPE=2 : 문자형 변수);
* ID 변수는 수치형 변수로 수정 불가;

if type=2 and name ne 'id';
* 임시로 변수명 변경을 위하여 신규 변수명 생성;
newname=trim(left(name))||"_n";
run;

 

options symbolgen;

proc sql noprint;
select trim(left(name)),
trim(left(newname)),
trim(left(newname))||'='||trim(left(name))
into :c_list separated by ' ',
:n_list separated by ' ',
:renam_list separated by ' '
from vars;
quit;

 

* 수치형 변수로 변경하기 위한 대상(문자형) 변수 리스트 생성;
%PUT &c_list;
* 변경되는 신규 수치형 변수 이름 리스트 - 임시로 사용;
%PUT &n_list;
* 임시로 지정한 변수 이름을 원래 변수 이름으로 변경하기 위하여 RENAME 구문에서 사용되는 변수 형태 지정;
* RENAME 임시 변수1 = 원본 변수1
임시 변수2 = 원본 변수2 -----;

%PUT &renam_list;

* 문자형 변수를 수치형 변수로 변경;
data test2;
set test;
array ch(*) $ &c_list;
array nu(*) &n_list;
* INPUT함수를 사용하여서 문자형 변수를 수치형 변수로 변경;
do i = 1 to dim(ch);
nu(i)=input(ch(i),8.);
end;
* 원본 문자형 변수 제거;
drop i &c_list;
* 임시로 지정된 수치형 변수 이름을 원래 변수 이름으로 변경;
rename &renam_list;
run;

 

* 원본 테이블의 변수명 순서로 재조정;
DATA TEST3;
RETAIN &TOTAL_list;
SET TEST2;
RUN;


 

[통계분석연구회] http://cafe.daum.net/statsas/3F8j/222

Version history
Last update:
‎06-11-2020 10:08 PM
Updated by:
Contributors

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Article Labels
Article Tags