全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
14078 14
2011-03-26
假设现在我已经建立了一个数据集a,里面有20个变量x1-x20,数据体a 中有20000个观测值。
1、第12个变量x12为字符型变量,数据集建立时设定的长度为20,现在我想将x12 的长度改为50,该如何实现?


2、现在我要删去第128个观测值,该如何实现?
二维码

扫码加我 拉你入群

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

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

全部回复
2011-3-28 21:36:45
length x12 $ 50;
format  x12 $50.;

if _n_=128 then delete;
二维码

扫码加我 拉你入群

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

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

2011-3-30 21:44:01
cadlag 发表于 2011-3-28 21:36
length x12 $ 50;
format  x12 $50.;

if _n_=128 then delete;
length x12 $ 50;
format  x12 $50.;


这种方式是不是会把X12原来的值都删掉?如果我想保留x12的值,只是改变它之前设定的格式该怎么办?
我自己建了一个数据库,原来设定的长度是25,后来发现长度太小,我想加到50,同时保留原来的信息。
二维码

扫码加我 拉你入群

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

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

2011-3-30 22:02:17
那个删除观测值的命令也不成功啊,请问cadlag,能不能把完整的命令写出来,用的是什么过程?
二维码

扫码加我 拉你入群

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

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

2011-3-31 10:34:30
cadlag的解答完全正确。
以下code复制到editor中即可:
data a;
length x12 $ 50;
set a;
format  x12 $50.;
if _n_=128 then delete;
run;
X12原来的值不会被删掉,因为length语句只是设定变量长度,而$50比原来的$20要长;
至于format语句是控制变量的显示格式,也要相应改成$50.。
二维码

扫码加我 拉你入群

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

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

2011-3-31 21:36:41
hehekaixin 发表于 2011-3-31 10:34
cadlag的解答完全正确。
以下code复制到editor中即可:
data a;
length x12 $ 50;
set a;
format  x12 $50.;
if _n_=128 then delete;
run;
X12原来的值不会被删掉,因为length语句只是设定变量长度,而$50比原来的$20要长;
至于format语句是控制变量的显示格式,也要相应改成$50.。
还是不行,这次我吧完整的结果贴出来,请大家看一下问题在哪里?
命令如下:
data peter;
length x y z $20;
input x y z;
cards;
a b c
e f g
h i j
;
run;

data _null_;
set peter;
put _all_;
run;


data peter;
length x $50;
run;
set peter;
format x $50;
if _n_=3 then delete;
run;

sas的log结果如下:

82   data peter;
83   length x y z $20;
84   input x y z;
85   cards;

NOTE: The data set WORK.PETER has 3 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


89   ;
90   run;

91   data _null_;
92   set peter;
93   put _all_;
94   run;

x=a y=b z=c _ERROR_=0 _N_=1
x=e y=f z=g _ERROR_=0 _N_=2
x=h y=i z=j _ERROR_=0 _N_=3
NOTE: There were 3 observations read from the data set WORK.PETER.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


95   data peter;
96   length x $50;
97   run;

NOTE: Variable x is uninitialized.
NOTE: The data set WORK.PETER has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


98   set peter;
     ---
     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

99   format x $50;
     ------
     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

100  if _n_=3 then delete;
     --
     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

101  run;
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群