楼主: liehuodgq
2754 4

[其他] 如何将SAS中数值的字符变量统一转变为数值型 [推广有奖]

  • 0关注
  • 0粉丝

高中生

92%

还不是VIP/贵宾

-

威望
0
论坛币
16 个
通用积分
0.0797
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
773 点
帖子
31
精华
0
在线时间
30 小时
注册时间
2015-11-23
最后登录
2021-11-20

楼主
liehuodgq 发表于 2015-12-25 13:59:35 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
data a;
input id $ x $ y $ z w $ t $ o $ p $ q $;
cards;
1 3 app 34 2 9 苹果 3 梨
4 6 people 56 8 9.6 4,8 cc 香蕉
12 6 t23 9 . . ztc4 4q 菠萝
15 89 pear 90 0 3 yourar er5 番茄
;run;

如上数据集,如何将其中为数值的字符变量统一转变为数值型?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:字符变量 数值型 People Input cards 如何

沙发
liehuodgq 发表于 2015-12-25 14:04:52
可以运用宏结合do语句,采用anyfirst及anydigit函数![tongue]

藤椅
liehuodgq 发表于 2015-12-25 14:08:20

板凳
liehuodgq 发表于 2015-12-25 14:12:06

热爱生活,感悟生活!

报纸
liehuodgq 发表于 2015-12-25 14:25:12



%macro vartonum();
proc sql noprint;
select max(varnum) into :maxno
       from sashelp.vcolumn
         where LIBNAME='WORK' AND MEMNAME='A';
QUIT;
%put &maxno;
proc sql noprint;
select name into :all_var_ separated by ' '
       from sashelp.vcolumn
         where LIBNAME='WORK' AND MEMNAME='A' ;
QUIT;
%put &all_var_;
%let all_var=%sysfunc(compress(&all_var_,id));
%put &all_var;
data b; set a;run;
data E; set a;run;
%do i=1 %to &maxno;
proc sql noprint;
select name into :vnam
       from sashelp.vcolumn
         where LIBNAME='WORK' AND MEMNAME='A' and varnum=&i;
QUIT;
data a_0;
  set a;
  if  anyfirst(&vnam) ne 0 and anydigit(&vnam) ne 1 then v_id=2;
  else if anyfirst(&vnam) =0 and anydigit(&vnam)=1 then v_id=1;
  else v_id=0;
run;
proc sql noprint;
  select avg(case when v_id=0 then . else v_id end) into :var_avg
        from a_0
;quit;
%put &var_avg;
data a;
   set a;
   if &var_avg=1 then aa_&i.=input(&vnam,8.);
run;
data b;
   set b;
   if &var_avg ne 1 then aa_&i.=&vnam.;
run;
%end;
data d;
  set a;
  drop &all_var_;
run;
proc sql noprint;
select name into :var_t_d separated by ' '
       from sashelp.vcolumn
         where LIBNAME='WORK' AND MEMNAME='D' and name ne 'id';
QUIT;
%put &var_t_d;
proc sql noprint;
select name into :var_t_a separated by ' '
       from sashelp.vcolumn
         where LIBNAME='WORK' AND MEMNAME='A' and name ne 'id';
QUIT;
%put &var_t_a;
proc sql noprint;
select name into :var_t_b separated by ' '
       from sashelp.vcolumn
         where LIBNAME='WORK' AND MEMNAME='A' and name ne 'id';
QUIT;
%put &var_t_b;
data c;
  set b;
  length missvar $30.;
  array char{*} _character_;
   do i=1 to dim(char);
     if missing(char{i}) then call catx(' ',missvar,vname(char{i}));
   end;
   drop i;
   if _n_=1 then call symput('missvar_',missvar);
run;
%put &missvar_;
proc sql noprint;
select name into :var_t_c separated by ' '
       from sashelp.vcolumn
         where LIBNAME='WORK' AND MEMNAME='C' and name ne 'id';
QUIT;
%put &var_t_c;
data b;
  set b;
  drop &all_var &missvar_;
run;
proc sql noprint;
select name into :var_t_B_ separated by ' '
       from sashelp.vcolumn
         where LIBNAME='WORK' AND MEMNAME='B' ;
QUIT;
%put &var_t_B_;
data a;
  set a;
  drop &var_t_B_;
run;
proc sql;
create table union_t as
  select a.*,b.* from a left join b on a.aa_1=input(compress(b.id),8.)
;quit;
data last_t;
retain &var_t_d;
  set union_t;
  drop &all_var id;
run;
proc sql noprint;
select compress(b.name||'='||a.name) into :renam separated by ' '
       from
        ( select varnum,name from sashelp.vcolumn where LIBNAME='WORK' AND MEMNAME='E') a
             left join
                   ( select varnum,name from sashelp.vcolumn where LIBNAME='WORK' AND MEMNAME='D') b
             on a.varnum=b.varnum
;quit;
%put &renam;
data a;
  set last_t;
  rename &renam;
run;
proc sql;
drop table a_0,b,c,d,e,last_t,union_t;
quit;
%mend;
%vartonum;


热爱生活,感悟生活!

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

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-28 20:12