Hello,
I have a question regarding sas data step modification. lets make my question clear using the following figure:
as you can see I have a data set similar to table (a) and I want to add a zero at the very first cell in the table (table (b)) and then shift all the cells. in the next step I need to sum up the coressponding cell values in new table (table(a)+table(b)=table(c) ). hope mt explanation is clear.
I am very new in SAS and I would be appriciated if somebody help me.
Thank you,
Samira
You can ask your company to buy IML license , it is very cheap .
And for your such Matrix Operator , the best way do is IML code.
If you insist to use Data Step, that will lead you to a lot more code.
data a;
input _2006 _2007 _2008;
cards;
1 5 1
1 5 2
3 2 2
4 3 2
;
run;
proc transpose data=a(obs=0) out=temp;
run;
data _null_;
set temp end=last;
if _n_=1 then call execute('data all;set ');
call execute(catt('a(keep=',_name_,' rename=(',_name_,'=var))'));
if last then call execute(';run;');
run;
data all;
set all;
new_var=sum(var,lag(var));
drop var;
run;
%let dsid=%sysfunc(open(a));
%let nobs=%sysfunc(attrn(&dsid,nlobs));
%let dsid=%sysfunc(close(&dsid));
data vname;
set temp;
do i=1 to &nobs;
output;
end;
drop i;
run;
data combine;
merge vname all;
run;
proc transpose data=combine out=temp1;
by _name_;
run;
proc transpose data=temp1 out=want(drop=_name_);
run;
Hello,
I have a question regarding sas data step modification. lets make my question clear using the following figure:
as you can see I have a data set similar to table (a) and I want to add a zero at the very first cell in the table (table (b)) and then shift all the cells. in the next step I need to sum up the coressponding cell values in new table (table(a)+table(b)=table(c) ). hope mt explanation is clear.
I am very new in SAS and I would be appriciated if somebody help me.
Thank you,
Samira
Since the values you show, 2006, 2007 and 2008 are not valid SAS variable names can you provide the names of your actual variables?
You say that you insert a zero, which is pretty clear for 2006 but is the "insert" for 2007 actually the bottom value for 2006? and the "insert" for 2008 the last 2007 value? And will there always be only 4 values in each column, such as quarters of a year?
Thank you for your reply,
actually I just wanted to make my dataset simple. here is my actual dataset:
You say that you insert a zero, which is pretty clear for 2006 but is the "insert" for 2007 actually the bottom value for 2006? yes that is correct
and the "insert" for 2008 the last 2007 value? yes that is also correct
And will there always be only 4 values in each column, such as quarters of a year? there will be always 53 rows and 12 columns (includs week)
It is about Matrix operator, Plz post it at IML forum.
data a;
input _2006 _2007 _2008;
cards;
1 5 1
1 5 2
3 2 2
4 3 2
;
run;
proc iml;
use a;
read all var _num_ into x[c=vnames];
close;
y=shapecol(0||remove(shapecol(x,0,1),nrow(x)#ncol(x)),0,ncol(x));
z=x+y;
print x[l='A' c=vnames],y[l='B' c=vnames],z[l='C' c=vnames];
quit;
You can ask your company to buy IML license , it is very cheap .
And for your such Matrix Operator , the best way do is IML code.
If you insist to use Data Step, that will lead you to a lot more code.
data a;
input _2006 _2007 _2008;
cards;
1 5 1
1 5 2
3 2 2
4 3 2
;
run;
proc transpose data=a(obs=0) out=temp;
run;
data _null_;
set temp end=last;
if _n_=1 then call execute('data all;set ');
call execute(catt('a(keep=',_name_,' rename=(',_name_,'=var))'));
if last then call execute(';run;');
run;
data all;
set all;
new_var=sum(var,lag(var));
drop var;
run;
%let dsid=%sysfunc(open(a));
%let nobs=%sysfunc(attrn(&dsid,nlobs));
%let dsid=%sysfunc(close(&dsid));
data vname;
set temp;
do i=1 to &nobs;
output;
end;
drop i;
run;
data combine;
merge vname all;
run;
proc transpose data=combine out=temp1;
by _name_;
run;
proc transpose data=temp1 out=want(drop=_name_);
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.