SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 739|回复: 2
打印 上一主题 下一主题

请教:大量字符变量转为数值变量

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-6-28 10:42:21 | 只看该作者

请教:大量字符变量转为数值变量

我有一个六千多个观测,四万多个变量的数据,所有变量的值都是字符型的数字,请问如何将所有变量的字符型都转为数值型,最好变量名不变。

谢谢!!!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2011-6-28 14:09:28 | 只看该作者

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]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2011-6-28 14:53:38 | 只看该作者

Re: 请教:大量字符变量转为数值变量

学习了,谢谢啦!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-6-10 05:41 , Processed in 0.109833 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表