BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BingL
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11

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. 

 

 

View solution in original post

6 REPLIES 6
mohamed_zaki
Barite | Level 11

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. 

 

 

BingL
Obsidian | Level 7

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!

 

Untitled.png

Reeza
Super User

Out of curiousity, what does your question title have to do with your question?

BingL
Obsidian | Level 7

Okay 

 

I will post this question in a new post.

Ksharp
Super User
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;
BingL
Obsidian | Level 7

Hello @Ksharp !

your code works as well

thank you!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 6 replies
  • 1838 views
  • 2 likes
  • 4 in conversation