BookmarkSubscribeRSS Feed
Cho8
Calcite | Level 5

@Ksharp wrote:

Do you have SAS/IML ? I would like to use IML code for this kind of question.

 

data have;
input y $ d1-d9;
datalines;
201201  0.01  0.00  0.01  0.00  0.00  0.00  0.01  0.01  0.01
201202  0.03  -0.01 0.02  0.01  0.00  0.00  0.01  0.00  0.04
201203  0.01  0.00  0.01  0.00  0.00  0.00  0.00  0.00  0.02
201204  0.02  0.01  0.00  0.01  0.02  0.00  0.00  0.00  0.01
201205  0.01  0.00  0.00  0.02  0.00  0.00  0.01  0.00  0.03
201206  0.00  0.01  0.01  0.02  0.00  0.00  0.00  0.00  0.01
201207  0.01  0.02  0.00  0.00  0.00  0.00  0.03  0.00  0.02
201208  0.01  0.00  0.00  0.01  0.01  0.00  0.02  0.02  0.00
201209  0.01  0.01  0.00  0.03  0.00  0.02  0.01  0.00  0.02
201210  0.02  0.00  0.00  0.01  0.04  0.03  0.02  0.01  0.04
201211  0.01  0.02  0.01  0.00  0.00  0.00  0.00  0.03  0.00
201212  0.02  0.00  0.01  0.00  0.01  0.06  0.00  0.02  0.03
201301  0.02  0.02  0.00  0.00  0.00  0.00  0.00  0.00  0.02
201302  0.00  0.00  0.01  0.00  0.00  0.01  0.00  0.01  0.05
201303  0.01  0.01  0.00  0.01  0.00  0.01  0.00  0.01  0.00
201304  0.00  0.04  0.01  0.01  0.01  0.03  0.02  0.04  -0.01
;

proc transpose data=have out=temp name=y prefix=_;
var d1-d9;
id y;
run;

data temp;
 set temp;
 array x{*} _:;
 count=0;
 do i=1 to 13;
  n=_n_+i;
  if n>dim(x) then leave;
  count+1;
  sum=sum(sum,x{n});
 end;
 _avg=sum/count;
 drop i n count sum;
run;

proc transpose data=temp out=want;
var _:;
id y;
run;



i dnt have SAS IML.. Need solution in Datastep or Macro

Cho8
Calcite | Level 5

@Cho8 wrote:

@Ksharp wrote:

Do you have SAS/IML ? I would like to use IML code for this kind of question.

 

data have;
input y $ d1-d9;
datalines;
201201  0.01  0.00  0.01  0.00  0.00  0.00  0.01  0.01  0.01
201202  0.03  -0.01 0.02  0.01  0.00  0.00  0.01  0.00  0.04
201203  0.01  0.00  0.01  0.00  0.00  0.00  0.00  0.00  0.02
201204  0.02  0.01  0.00  0.01  0.02  0.00  0.00  0.00  0.01
201205  0.01  0.00  0.00  0.02  0.00  0.00  0.01  0.00  0.03
201206  0.00  0.01  0.01  0.02  0.00  0.00  0.00  0.00  0.01
201207  0.01  0.02  0.00  0.00  0.00  0.00  0.03  0.00  0.02
201208  0.01  0.00  0.00  0.01  0.01  0.00  0.02  0.02  0.00
201209  0.01  0.01  0.00  0.03  0.00  0.02  0.01  0.00  0.02
201210  0.02  0.00  0.00  0.01  0.04  0.03  0.02  0.01  0.04
201211  0.01  0.02  0.01  0.00  0.00  0.00  0.00  0.03  0.00
201212  0.02  0.00  0.01  0.00  0.01  0.06  0.00  0.02  0.03
201301  0.02  0.02  0.00  0.00  0.00  0.00  0.00  0.00  0.02
201302  0.00  0.00  0.01  0.00  0.00  0.01  0.00  0.01  0.05
201303  0.01  0.01  0.00  0.01  0.00  0.01  0.00  0.01  0.00
201304  0.00  0.04  0.01  0.01  0.01  0.03  0.02  0.04  -0.01
;

proc transpose data=have out=temp name=y prefix=_;
var d1-d9;
id y;
run;

data temp;
 set temp;
 array x{*} _:;
 count=0;
 do i=1 to 13;
  n=_n_+i;
  if n>dim(x) then leave;
  count+1;
  sum=sum(sum,x{n});
 end;
 _avg=sum/count;
 drop i n count sum;
run;

proc transpose data=temp out=want;
var _:;
id y;
run;



i dnt have SAS IML.. Need solution in Datastep or Macro


i didnot get the logic of array part..can you just explain it..

Ksharp
Super User
array x{*} _:; /*Refer to variables _201201 _201202 _201203 ...........*/
count=0; /* how many obs should be used for MEAN*/
do i=1 to 13; /*Rolling windows */
n=_n_+i; /* when d1(first obs) start with 1+1=2 ,then 1+2=3 1+3=4 ......
when d2(second obs) start with 2+1=3 ,then 2+2=4 2+3=5 ....*/

if n>dim(x) then leave; /*If the length of windows is longer than the number of obs,
then don't run the following code, and enter the next loop*/

count+1; /*how many obs have been used for calculated AVG*/
sum=sum(sum,x{n}); /*calculated SUM of 201202 - 201302 for d1 */
end;
_avg=sum/count; /*calculated avg/mean */
Cho8
Calcite | Level 5

If missing value, i want to next value.. i.e count start from non-missing value

Ksharp
Super User
There is no missing value only 0 .
And Due to it is old post, plz start a new session/topic .
Cho8
Calcite | Level 5
XY0Y1Y2Y3Y4Y5Y6Y7Y8Y9
2012011%0%1%0%0%(0%)....
2012023%(1%)2%1%0%0%1%...
2012031%0%1%0%(0%)0%0%0%..
2012042%1%0%1%2%0%(0%)(0%)1%.
2012051%0%0%2%(0%)0%1%0%3%1%
2012060%1%1%2%0%(0%)0%(0%)1%3%
2012071%2%(0%)(0%)0%0%3%0%2%(0%)
2012081%(0%)0%1%1%0%2%2%(0%)1%
2012091%1%(0%)3%0%2%1%(0%)2%3%
2012102%(0%)(0%)1%4%3%2%1%4%4%
2012111%2%1%(0%)0%(0%)0%3%0%0%
2012122%0%1%(0%)1%6%0%2%3%(0%)
2013012%2%0%(0%)0%0%0%(0%)2%(0%)
2013020%0%1%0%0%1%(0%)1%5%(1%)
2013031%1%0%1%0%1%0%1%(0%)1%
2013040%4%1%1%1%3%2%4%(1%)1%
AVG0.0129210.0048530.0054520.0081070.0069150.0096270.0082070.0072930.018898

0.011633

 

 

 

Ksharp
Super User

Here is IML code , if you like it.

 

data have;
input y $ d1-d9;
datalines;
201201  0.01  0.00  0.01  0.00  0.00  0.00  0.01  0.01  0.01
201202  0.03  -0.01 0.02  0.01  0.00  0.00  0.01  0.00  0.04
201203  0.01  0.00  0.01  0.00  0.00  0.00  0.00  0.00  0.02
201204  0.02  0.01  0.00  0.01  0.02  0.00  0.00  0.00  0.01
201205  0.01  0.00  0.00  0.02  0.00  0.00  0.01  0.00  0.03
201206  0.00  0.01  0.01  0.02  0.00  0.00  0.00  0.00  0.01
201207  0.01  0.02  0.00  0.00  0.00  0.00  0.03  0.00  0.02
201208  0.01  0.00  0.00  0.01  0.01  0.00  0.02  0.02  0.00
201209  0.01  0.01  0.00  0.03  0.00  0.02  0.01  0.00  0.02
201210  0.02  0.00  0.00  0.01  0.04  0.03  0.02  0.01  0.04
201211  0.01  0.02  0.01  0.00  0.00  0.00  0.00  0.03  0.00
201212  0.02  0.00  0.01  0.00  0.01  0.06  0.00  0.02  0.03
201301  0.02  0.02  0.00  0.00  0.00  0.00  0.00  0.00  0.02
201302  0.00  0.00  0.01  0.00  0.00  0.01  0.00  0.01  0.05
201303  0.01  0.01  0.00  0.01  0.00  0.01  0.00  0.01  0.00
201304  0.00  0.04  0.01  0.01  0.01  0.03  0.02  0.04  -0.01
;
proc iml;
use have(keep=d:) nobs nobs;
read all var _num_ into x[c=vname] ;
close;
avg=j(1,ncol(x),.);
do i=1 to ncol(x);
  l=i+1;
  u=(i+13)><nobs;
  avg[i]=x[l:u,i][:];
end;
create avg from avg[c=vname];
append from avg;
close;
quit;

data want;
 set have avg ;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 21 replies
  • 2669 views
  • 0 likes
  • 5 in conversation