楼主: julius725
7795 18

[SAS] 累加问题,拜求SAS达人 [推广有奖]

  • 0关注
  • 0粉丝

本科生

3%

还不是VIP/贵宾

-

威望
0 级
论坛币
5 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
105 点
帖子
41
精华
0
在线时间
88 小时
注册时间
2007-5-20
最后登录
2025-5-18

楼主
julius725 发表于 2008-10-15 22:27:00 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
<p>SAS里如何对一个变量做累次求和?比如:</p><p>变量x依次取值1、2、3、4、5、6,现要输出一个新变量y,使得y的取值为1,1+2,1+2+3,……1+2+3+4+5+6?</p><p>SAS好像在data步对列求和很难,不知哪位达人能帮小弟解答一下。感激不尽~</p>
二维码

扫码加我 拉你入群

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

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

关键词:data步 感激不尽 Data 新变量 如何

回帖推荐

风丽 发表于6楼  查看完整内容

suppose a is the original dataset name with the column names x1, x2, x3, x4, x5,x6.then it is very simple to get the data you needed.data a; set a; y1 = x1;y2 = y1 + x2;y3 = y2 + x3;y4 = y3 + x4;y5 = y4 + x5;y6 = y5 + x6;run;then, y1,y2,...,y6 should have the data you wanted.SAS has a different style with the other program languages such as VC,VB.

追梦天石100 发表于3楼  查看完整内容

data ;p=0;do i=1 to 100;p=sum(p,i);output;end;run;proc print;run;

宜桦 发表于2楼  查看完整内容

data one;retain y;if _n_ = 1 then y = 0;do x = 1 to 10;  y = sum(x, y);  y = sum(x, y);  output;end;run; [此贴子已经被作者于2008-10-15 23:18:35编辑过]

本帖被以下文库推荐

沙发
宜桦 发表于 2008-10-15 23:16:00
<b><font face="Courier New" color="#000080" size="2"><font face="Courier New" color="#000080" size="2"><font face="Courier New" color="#000080" size="2"><p>data<font face="Courier New" size="2"><font face="Courier New" size="2"> one;</font></font></p></font></font></font></b><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><p>retain<font face="Courier New" size="2"><font face="Courier New" size="2"> y;</font></font></p></font></font></font><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><p>if<font face="Courier New" size="2"><font face="Courier New" size="2"> _n_ = </font></font><b><font face="Courier New" color="#008080" size="2"><font face="Courier New" color="#008080" size="2"><font face="Courier New" color="#008080" size="2">1</font></font></font></b><font face="Courier New" size="2"><font face="Courier New" size="2">&nbsp;</font></font><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2">then</font></font></font><font face="Courier New" size="2"><font face="Courier New" size="2"> y = </font></font><b><font face="Courier New" color="#008080" size="2"><font face="Courier New" color="#008080" size="2"><font face="Courier New" color="#008080" size="2">0;</font></font></font></b></p></font></font></font><font face="Courier New" size="2"><font face="Courier New" size="2"></font></font><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><p>do<font face="Courier New" size="2"><font face="Courier New" size="2"> x = </font></font><b><font face="Courier New" color="#008080" size="2"><font face="Courier New" color="#008080" size="2"><font face="Courier New" color="#008080" size="2">1 to</font></font></font></b><font face="Courier New" size="2"><font face="Courier New" size="2">&nbsp;</font></font><b><font face="Courier New" color="#008080" size="2"><font face="Courier New" color="#008080" size="2"><font face="Courier New" color="#008080" size="2">10</font></font></font></b><font face="Courier New" size="2"><font face="Courier New" size="2">;<p>&nbsp; y = sum(x, y);</p></font></font></p><p>&nbsp; y = sum(x, y);</p><p></p></font></font></font><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><p>&nbsp; output<font face="Courier New" size="2"><font face="Courier New" size="2">;</font></font></p></font></font></font><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><font face="Courier New" color="#0000ff" size="2"><p>end<font face="Courier New" size="2"><font face="Courier New" size="2">;</font></font></p></font></font></font><b><font face="Courier New" color="#000080" size="2"><font face="Courier New" color="#000080" size="2"><font face="Courier New" color="#000080" size="2"><p>run<font face="Courier New" size="2"><font face="Courier New" size="2">;</font></font></p></font></font></font></b>

[此贴子已经被作者于2008-10-15 23:18:35编辑过]

已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

藤椅
追梦天石100 发表于 2008-10-15 23:23:00
data ;<br/>p=0;<br/>do i=1 to 100;<br/>p=sum(p,i);<br/>output;<br/>end;<br/>run;<br/>proc print;<br/>run;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

板凳
qwqwqw 发表于 2008-10-16 00:53:00
data a;<br/>do x=1 to 5;<br/>y=0.5*(1+x)*x;<br/>output;<br/>end;<br/>run;

报纸
julius725 发表于 2008-10-16 21:46:00
<p>多谢各位相助,只是小弟举例中的数字比较特殊,可以用数列求和的方式实现,若一般的情况,比如x为连续十年的粮食产量,对其进行累加求和,该如何实现呢?</p>

地板
风丽 发表于 2008-10-17 10:44:00
<p>suppose a is the original dataset name&nbsp;with the column names x1, x2, x3, x4, x5,x6.</p><p>then it is very simple to get the data you needed.</p><p>data a; set a; </p><p>y1 = x1;</p><p>y2 = y1 + x2;</p><p>y3 = y2 + x3;</p><p>y4 = y3 + x4;</p><p>y5 = y4 + x5;</p><p>y6 = y5 + x6;</p><p>run;</p><p>then, y1,y2,...,y6 should have the data you wanted.</p><p>SAS has a different style with the other program languages such as VC,VB.</p><p></p>
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

7楼
julius725 发表于 2008-10-17 14:28:00
<p>谢谢!只是这样做还是对行求和,我说一下我的问题吧:</p><p>变量x为1970-2007年我国年粮食产量,于是就有38个观测值,如何对它们进行累加呢?</p><p>要是像你这样直接写,得写30行左右的程序,但用循环似乎也不行,因为这些数据都在一个变量里啊~</p>

8楼
alexander523 发表于 2008-10-17 16:56:00
iml用矩阵

9楼
fanly111 发表于 2008-10-17 19:01:00
data a;<br/>infile datalines;<br/>input x@@;<br/>datalines;<br/>1 2 3 4 5 6<br/>;<br/>run;<br/>data b;<br/>set a;<br/>retain y;<br/>y+x;<br/>run;

10楼
fanly111 发表于 2008-10-17 19:13:00
<p>input production@@;<br/>cards;<br/>100 105 110 115 120 125 130 135 140 145 150 155 160<br/>165 170 175 180 185 190 195 200 205 210 215 220 225 <br/>230 235 240 245 250 255 260 265 270 275 280 285 <br/>;<br/>run;</p><p>proc means data=production n sum;<br/>var production;<br/>output out=sum_production sum(production)=;<br/>run;</p>

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

客服
GMT+8, 2026-12-5 01:21