Hello To whomever can help me out
There are 20 variables in my data. I will need to do the same calculation for each one of them.
So how should I write a Macro that will repeat calculation for all them variable like the following code for one variable:
data Portfolio;
set Portfolio;
Return1 = log(lag(variable1)/variable1));
run;
Thank you
Actually for what you want _based on your description_...You do not need Macro. You can do it in one data step using array like:
data have;
input b d f h j k ;
cards;
2 3 4 6 88 9
2 3 4 6 88 9
2 3 4 6 88 9
6 7 8 0 4 3
;
run;
data want ;
set have;
array Variable{6} b -- i; /* Your fist variable (b) till your last variable (i)*/
array ReturnV {6};
do i= 1 to 6;
ReturnV[i]=log(lag(Variable[i]) / Variable[i]);
end;
run;
Just change the size to 20.
Actually for what you want _based on your description_...You do not need Macro. You can do it in one data step using array like:
data have;
input b d f h j k ;
cards;
2 3 4 6 88 9
2 3 4 6 88 9
2 3 4 6 88 9
6 7 8 0 4 3
;
run;
data want ;
set have;
array Variable{6} b -- i; /* Your fist variable (b) till your last variable (i)*/
array ReturnV {6};
do i= 1 to 6;
ReturnV[i]=log(lag(Variable[i]) / Variable[i]);
end;
run;
Just change the size to 20.
Hello Zaki
Thank you for the solution
now how should i write a macro to rename those new variables as CVS_ret, VZ_ret, PEP_ret, XOM_ret etc.?
Thank you!
Out of curiousity, what does your question title have to do with your question?
Okay
I will post this question in a new post.
data have;
input b d f h j i ;
cards;
2 3 4 6 88 9
2 3 4 6 88 9
2 3 4 6 88 9
6 7 8 5 4 3
;
run;
proc transpose data=have(obs=0) out=temp;
var _all_;
run;
proc sql;
select cats(_name_,'_ret=log(lag(',_name_,')/',_name_,')') into : list separated by ';'
from temp;
quit;
data want;
set have;
&list ;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.