标题: [求助]还是数据查找输出问题 [打印本页] 作者: shiyiming 时间: 2009-10-27 13:01 标题: [求助]还是数据查找输出问题 [code:2lbp5w1x]
data aa;
input name$ quote_id$;
datalines;
AA a000
AA a000
AA a001
AA a002
BB b003
BB b003
BB b003
BB b004
BB b005
CC c006
CC c007
CC c008
CC c008
CC c008
;
[/code:2lbp5w1x]
这样的数据,我想把含有同样quote_id的数据输出,即:
AA a000
AA a000
BB b003
BB b003
BB b003
CC c008
CC c008
CC c008
do _n_ = 1 by 1 until (last.quote_id);
set aa;
by quote_id;
if first.quote_id then lagq=.;
else lagq=lag(quote_id);
end;
if quote_id=lagq then qflag=1;
else qflag=0;
run;
[/code:2lbp5w1x]作者: shiyiming 时间: 2009-10-27 13:54 标题: Re: [求助]还是数据查找输出问题 [code:1aumtnjr]proc sort data=aa;
by name quote_id;
run;
data aa;
set aa;
by name quote_id;
if first.quote_id and last.quote_id then delete;
run;[/code:1aumtnjr]作者: shiyiming 时间: 2009-10-27 14:40 标题: Re: [求助]还是数据查找输出问题 wow,原来如此简单,谢谢!!!
不过请问这个first.xxx 在系统中会是个什么值?last.xxx呢?
if first.xxx and last.xxx 可以表示成first.xxx=last.xxx吗?
再次感谢!!!作者: shiyiming 时间: 2009-10-27 17:17 标题: 继续求助Re: [求助]还是数据查找输出问题 如果输出的data set中还有别的variable
比如:
name quote_id var1 var2
AA a000 4 5
AA a000 4 5
BB b003 2 3
BB b003 2 3
BB b003 . .
CC c008 1 1
CC c008 1 0
CC c008 . 0
谢谢!作者: shiyiming 时间: 2009-10-27 19:39 标题: Re: [求助]还是数据查找输出问题 抛砖引玉
[code:1e44u4y7]data aa;
input name $ quote_id $ var1 var2;
cards;
AA a000 4 5
AA a000 4 5
BB b003 2 3
BB b003 2 3
BB b003 . .
CC c008 1 1
CC c008 1 0
CC c008 . 0
;
run;
proc sort data=aa;
by name quote_id;
run;
data aa;
set aa;
by name quote_id;
if first.quote_id and last.quote_id then delete;
run;
proc sort data=aa;by name quote_id descending var1 descending var2;run; /*以最大为参考值*/
data dif;
set aa;
by name quote_id;
array var{*} var1-var2;
array f_var{*} f_var1-f_var2;
array dif{*} dif1-dif2;
retain f_var1-f_var2;
if first.quote_id then do;
do i=1 to 2;
f_var(i)=var(i);
dif(i)=0;/*只有0代表无差别*/
end;
end;
else do;
do i=1 to 2;
if f_var(i)=. then dif(i)=0;
else dif(i)=var(i)-f_var(i);/*与最大值的差别*/;
end;
end;
drop i f_:;
run;
proc print;run;[/code:1e44u4y7]作者: shiyiming 时间: 2009-10-27 20:53 标题: Re: [求助]还是数据查找输出问题 谢谢楼上牛人,现在又出现个问题。
74 data dif;
75 set samequoteid;
76 by ticker quoteid;
77 array var{*} var1-var15;
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
78 array f_var{*} f_var1-f_var15;
79 array dif{*} dif1-dif15;
80 retain f_var1-f_va15;
ERROR: Alphabetic prefixes for enumerated variables (f_var1-f_va15) are different.
81 if first.quoteid then do;
82 do i=1 to 15;
83 f_var(i)=var(i);
WARNING: The array var has the same name as a SAS-supplied or user-defined function. Parenthesized
references involving this name have been treated as array references and not function
references.
84 dif(i)=0;/*??0?????*/
WARNING: The array dif has the same name as a SAS-supplied or user-defined function. Parenthesized
references involving this name have been treated as array references and not function
references.
85 end;
86 end;
87 else do;
88 do i=1 to 15;
89 if f_var(i)=. then dif(i)=0;
90 else dif(i)=var(i)-f_var(i);/*???????*/;
91 end;
92 end;
93 drop i f_:;
94 run;
NOTE: Character values have been converted to numeric values at the places given by:
(Line):(Column).
83:9 90:21
NOTE: The SAS System stopped processing this step because of errors.
有什么好办法?作者: shiyiming 时间: 2009-10-28 11:27 标题: Re: [求助]还是数据查找输出问题 如果不太讲究,可以用proc compare将就一下
[code:3psjwi00]data raw;
input name $ quote_id $ var1 var2 var3 $;
datalines;
AA a000 4 5 A
AA a000 4 5 V
BB b003 2 3 A
BB b003 2 3 A
BB b003 . . V
CC c008 1 1 A
CC c008 1 0 A
CC c008 . 0 V
;
proc sort data=raw;
by name quote_id;
run;
data std;
set raw;
by name quote_id;
if first.quote_id then output;
run;
data std;
merge std raw(keep=name quote_id);
by name quote_id;
run;
proc compare base=std compare=raw nosummary;
by name quote_id;
run;[/code:3psjwi00]作者: shiyiming 时间: 2009-10-28 11:53 标题: Re: [求助]还是数据查找输出问题 要在SAS中同时运算字符和数值型变量,我现在所知道方法可能只有用宏。但是针对15个变量进行重复操作,可能要降低运算速度。HOPEWELL的方法可能是目前最适合的方法了。作者: shiyiming 时间: 2009-10-28 14:01 标题: Re: [求助]还是数据查找输出问题 hopewell 大侠巧妙的用proc compare 实现了多条observations之间的比较,应该是最好的解决方法了。如果要找出字符型变量详细的差别位置,可以把compare的结果output出来打印。作者: shiyiming 时间: 2009-10-28 21:15 标题: Re: [求助]还是数据查找输出问题 Do not look for non-standard solutions for standard questions unless you get a neater one. Keep it simple.
[code:183tos1c]data raw;
input name $ quote_id $ var1 var2 var3 $;
datalines;
AA a000 4 5 A
AA a000 4 5 V
BB b003 2 3 A
BB b003 2 3 A
BB b003 . . V
CC c008 1 1 A
CC c008 1 0 A
CC c008 . 0 V
;
proc sql;
select distinct *,count(*) as dupNum
from raw
group by name,quote_id,var1,var2,var3
;[/code:183tos1c]作者: shiyiming 时间: 2009-10-28 22:03 标题: Re: [求助]还是数据查找输出问题 <!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D -->作者: shiyiming 时间: 2009-10-30 14:17 标题: Re: [求助]还是数据查找输出问题 大虾们太厉害了,让我一一运行一下看看有没有什么差别。