How do I write a macro to...import multiple text files that have the same format?

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

How do I write a macro to...import multiple text files that have the same format?

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


Accepted Solutions
Solution
‎01-24-2016 11:45 AM
Super Contributor
Posts: 490

Re: How do I write a macro to...import multiple text files that have the same format?

[ Edited ]

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


All Replies
Solution
‎01-24-2016 11:45 AM
Super Contributor
Posts: 490

Re: How do I write a macro to...import multiple text files that have the same format?

[ Edited ]

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. 

 

 

Contributor
Posts: 31

Re: How do I write a macro to...import multiple text files that have the same format?

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

Grand Advisor
Posts: 17,342

Re: How do I write a macro to...import multiple text files that have the same format?

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

Contributor
Posts: 31

Re: How do I write a macro to...import multiple text files that have the same format?

Okay 

 

I will post this question in a new post.

Grand Advisor
Posts: 9,578

Re: How do I write a macro to...import multiple text files that have the same format?

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;
Contributor
Posts: 31

Re: How do I write a macro to...import multiple text files that have the same format?

Hello @Ksharp !

your code works as well

thank you!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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