在SAS中怎么批量地将数值型转化为字符型

 我来答
yugao1986
2012-03-14 · TA获得超过1129个赞
知道小有建树答主
回答量:449
采纳率:100%
帮助的人:147万
展开全部
sas官网的例子:它是将数据中所有的字符型转变为数值型,相应变化下就可以解决你的问题。
/*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. */

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. A new list of numeric*/
/*variable names is created from the character variable name with a "_n" */
/*appended to the end of each name. */

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

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

options symbolgen;

/*PROC SQL is used to create three macro variables with the INTO clause. One */
/*macro variable named c_list will contain a list of each character variable */
/*separated by a blank space. The next macro variable named n_list will */
/*contain a list of each new numeric variable separated by a blank space. 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. */

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;

/*The DATA step is used to convert the numeric values to character. An ARRAY */
/*statement is used for the list of character variables and another ARRAY for */
/*the list of numeric variables. A DO loop is used to process each variable */
/*to convert the value from character to numeric with the INPUT function. 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. */

data test2;
set test;
array ch(*) $ &c_list;
array nu(*) &n_list;
do i = 1 to dim(ch);
nu(i)=input(ch(i),8.);
end;
drop i &c_list;
rename &renam_list;
run;
追问
有没有更简单的方法,这个到后面的话,就看不太懂了,我想是不是可以直接用数组的形式批量修改呢,而且上面的这个程序好像是批量地将字符型转化为数值型
追答
这个代码是更一般的,它的原理和你想的一样,利用数组批量更改。你只需要根据这个代码进行character和numeric变量进行变换下就行了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式