data a;
input a g h;
cards;
1 1 1
2 2 1
3 4 1
4 3 2
5 5 3
6 6 4
7 2 5
;
run;
if g=1,2,3,4,5,6 分别赋值6.0 5.4 4.2 3.1 2.2 1.0分
if h=1 and g=1,给h赋值6分,if h=1 and 1<g<7给h赋值5分
if h=2 and 0<g<7给h赋值4分,if h=3 and 0<g<7给h赋值3分
if h=4 and 0<g<7给h赋值2分,if h=5 and 0<g<7给h赋值1分
现在要求g,h各自赋值后的和,SAS程序如何实现?
求教各位大虾了,谢谢。[/color:3ntzjjic]作者: Qiong 时间: 2009-7-16 17:26 标题: Re: 求教:给两个变量赋分后求和(变量关系不同赋分值不同),如何实现? 不是大虾,但是把你的几个条件变成最最基本if语句就行了阿~~~
难道有啥玄机我没有看出来?作者: shiyiming 时间: 2009-7-16 21:14 标题: Re: 求教:给两个变量赋分后求和(变量关系不同赋分值不同),如何实现? data a;
input a g h;
cards;
1 1 1
2 2 1
3 4 1
4 3 2
5 5 3
6 6 4
7 2 5
;
run;
data b;
set a;
if g=1 then g=6.0;
else if g=2 then g=5.4;
else if g=3 then g=4.2;
else if g=4 then g=3.1;
else if g=5 then g=2.2;
else g=1.0
;
run;
data c;
set b;
if (h=1 and g=1) then lh=6;
else if (h=1 and g>1 and g<7) then lh=5;
else if (h=2 and g>0 and g<7) then lh=4;
else if (h=3 and g>0 and g<7) then lh=3;
else if (h=4 and g>0 and g<7) then lh=2;
else if (h=5 and g>0 and g<7) then lh=1;
run;
data end1 ;
set c;
end1=g+lh;
keep a g lh end1;
run;
end1即为所求。作者: shiyiming 时间: 2009-7-17 09:04 标题: Re: 求教:给两个变量赋分后求和(变量关系不同赋分值不同),如何实现? [color=#0000BF:z9m27nuo]谢谢楼上两位,原本不想写那么多if语句的,不过现在看来只能这样了。[/color:z9m27nuo]作者: shiyiming 时间: 2009-7-17 10:18 标题: Re: 求教:给两个变量赋分后求和(变量关系不同赋分值不同),如何实现? [code:2bmu5xv0]data fmt_data;
input fmtname $ start end label $;
datalines;
g_fmt 1 1 6.0
g_fmt 2 2 5.4
g_fmt 3 3 4.2
g_fmt 4 4 3.1
g_fmt 5 5 2.2
g_fmt 6 6 1.0
hg_fmt 1 1 g1_fmt.
hg_fmt 2 2 g2_fmt.
hg_fmt 3 3 g3_fmt.
hg_fmt 4 4 g4_fmt.
hg_fmt 5 5 g5_fmt.
g1_fmt 1 1 6
g1_fmt 2 7 5
g2_fmt 0 7 4
g3_fmt 0 7 3
g4_fmt 0 7 2
g5_fmt 0 7 1
;