SAS中文论坛

标题: 请教:大量字符变量转为数值变量 [打印本页]

作者: shiyiming    时间: 2011-6-28 10:42
标题: 请教:大量字符变量转为数值变量
我有一个六千多个观测,四万多个变量的数据,所有变量的值都是字符型的数字,请问如何将所有变量的字符型都转为数值型,最好变量名不变。

谢谢!!!
作者: shiyiming    时间: 2011-6-28 14:09
标题: Re: 请教:大量字符变量转为数值变量
<!-- m --><a class="postlink" href="http://support.sas.com/kb/40/700.html">http://support.sas.com/kb/40/700.html</a><!-- m -->
“How to convert all character variables to numeric and use the same variable names in the output data set”
[code:ummbkiil]/*The sample data set TEST contains both character and numeric variables*/

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                                       
;                                                         
/*PROC CONTENTS is used to create an output data set called VARS to list all */
/*variable names and their type from the TEST data set&#46;                      */                                                         

proc contents data=test out=vars(keep=name type) noprint;

/*A DATA step is used to subset the VARS data set to keep only the character */
/*variables and exclude the one ID character variable&#46;  A new list of numeric*/
/*variable names is created from the character variable name with a &quot;_n&quot;     */
/*appended to the end of each name&#46;                                          */                                                        

data vars;                                                
set vars;                                                
if type=2 and name ne 'id';                              
newname=trim(left(name))||&quot;_n&quot;;                                                                              

/*The macro system option SYMBOLGEN is set to be able to see what the macro*/
/*variables resolved to in the SAS log&#46;                                    */                                                      

options symbolgen;                                       

/*PROC SQL is used to create three macro variables with the INTO clause&#46;  One  */
/*macro variable named c_list will contain a list of each character variable   */
/*separated by a blank space&#46;  The next macro variable named n_list will       */
/*contain a list of each new numeric variable separated by a blank space&#46;  The */
/*last macro variable named renam_list will contain a list of each new numeric */
/*variable and each character variable separated by an equal sign to be used on*/
/*the RENAME statement&#46;                                                        */                                                        

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

/*The DATA step is used to convert the numeric values to character&#46;  An ARRAY  */
/*statement is used for the list of character variables and another ARRAY for  */
/*the list of numeric variables&#46;  A DO loop is used to process each variable   */
/*to convert the value from character to numeric with the INPUT function&#46;  The */
/*DROP statement is used to prevent the character variables from being written */
/*to the output data set, and the RENAME statement is used to rename the new   */
/*numeric variable names back to the original character variable names&#46;        */                                                        

data test2;                                               
set test;                                                
array ch(*) $ &amp;c_list;                                    
array nu(*) &amp;n_list;                                      
do i = 1 to dim(ch);                                      
  nu(i)=input(ch(i),8&#46;);                                 
end;                                                      
drop i &amp;c_list;                                          
rename &amp;renam_list;                                                                                      
run;            
[/code:ummbkiil]
作者: shiyiming    时间: 2011-6-28 14:53
标题: Re: 请教:大量字符变量转为数值变量
学习了,谢谢啦!




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2