全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1660 6
2012-11-10
I have a question about inserting data in SAS.
For example, I have a data set:

data a;
input   n   x;
cards;
1   10                                                                                       
2   11.5                                                                                                                                 
4   12                                                                                                                                    
5   10.6                                                                                                                                 
7   9.8
;
run;

As there is no data for n=3,6, then I want to create the value n=3 and n=6 with coressponding x=(11.5+12)/2 and x=(10.6+9.8)/2. How can I do so?
The final data set should look like this:

n   x
1   10
2   11.5   
3   11.75                                                                                                                              
4   12                                                                                                                                    
5   10.6         
6   10.2                                                                                                                        
7   9.8

二维码

扫码加我 拉你入群

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

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

全部回复
2012-11-10 18:06:43
data b(drop=x_1 x_2);
retain x_1 x_2;
set a;
output;
if  n in (4,7) then
  do;
    n=n-1;
    x=(x+x_2)/2;
    output;
  end;
x_1=x;
x_2=x_1;
run;
proc sort;
by n;
run;
proc print;
run;
data_b.png

二维码

扫码加我 拉你入群

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

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

2012-11-10 23:12:14
楼上高人,学习
二维码

扫码加我 拉你入群

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

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

2012-11-11 16:24:03
davil2000 发表于 2012-11-10 18:06
data b(drop=x_1 x_2);
retain x_1 x_2;
set a;
Thank a lot! What is the purpose of creating x_2? Why can't I simply use x=(x+x_1)/2?
二维码

扫码加我 拉你入群

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

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

2012-11-11 16:49:42
lam_fukming 发表于 2012-11-11 16:24
Thank a lot! What is the purpose of creating x_2? Why can't I simply use x=(x+x_1)/2?
pls run the codes below, then you can find your results.

data b;
retain x_1 x_2;
set a;
output;
if  n in (4,7) then
  do;
    n=n-1;
    x=(x+x_2)/2;
    output;
  end;
x_1=x;
x_2=x_1;
run;

proc print;
run;
二维码

扫码加我 拉你入群

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

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

2012-11-11 20:24:38
davil2000 发表于 2012-11-11 16:49
pls run the codes below, then you can find your results.

data b;
I use the same method for data with different gaps, but the result is not exactly the same as what I expect.
For example,

data a;                                                                                                                                   
input   n   x;                                                                                                                           
cards;                                                                                                                                    
1   10                                                                                                                                    
2   11.5                                                                                                                                 
4   12                                                                                                                                    
5   10.6                                                                                                                                 
7   9.8                                                                                                                                   
10   10.1                                                                                                                                 
11  12                                                                                                                                    
;                                                                                                                                         
run;                                                                                                                                      

data b;                                                                                                                                   
retain x_1 x_2;                                                                                                                           
set a;                                                                                                                                    
output;                                                                                                                                   
if  _n_ in (3,5) then                                                                                                                     
   do;                                                                                                                                    
    n=n-1;                                                                                                                                
    x=(x+x_2)/2;                                                                                                                          
    output;                                                                                                                              
  end;                                                                                                                                    
if  _n_ in (6) then                                                                                                                       
   do j=1 to 2;                                                                                                                           
    n=n-1;                                                                                                                                
    x=(x_2)*j/3 + x*(3-j)/3;                                                                                                                     
    output;                                                                                                                              
  end;                                                                                                                                    

x_1=x;                                                                                                                                    
x_2=x_1;                                                                                                                                 
run;                                                                                                                                      

proc sort data=b;                                                                                                                        
by n;                                                                                                                                    
run;                                                                                                                                      
proc print;                                                                                                                              
run;

The result is as follow:
1.png

It is different from the expected result for n= 8, 9

n      x
8      9.9
9      10

二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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