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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 923 views
  • 2 likes
  • 4 in conversation