Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write a Macro for the following SAS code

Status
Not open for further replies.

smarathe

MIS
Feb 28, 2011
3
0
0
US
Hi,

I am actually learning SAS by myself. Can you please help me in creating a macro for this program using infile function instead of using the datalines.


Data file will have the following data. Strore it as problem1.txt

0 1 1 0 -2 6 7 7 4 -3 9 14


Write a macro and store it to compute the sum for the following program

The SAS program without macro goes here.

/* Fisher Randomization test */
Code:
option ls=78;

data xx;

	input y1-y12;
		array ss(*) y1-y12;
	m=4; nn=12;
	
	do n1=1 to nn-m+1;
	   do n2=n1+1 to nn-m+2;
	   do n3=n2+1 to nn-m+3;
	      do n4=n3+1 to nn-m+4;
	      do n5=n4+1 to nn-m+5;

	sum=ss(n1)+ss(n2)+ss(n3)+ss(n4)+ss(n5);
	  if sum <=0 then t=1; else t=0;
		output;
	end; end; end; end; end;


datalines;

0 1 1 0 -2 6 7 7 4 -3 9 14

run;

/* The main objective is instead of using datalines here, store the data in a file and use infile keyword */

proc univariate noprint;
var t;
	output out=temp n=nn sum=tt;

	data xx;
	set temp;
	   pp=tt*2/nn;
	   proc print data=xx;
	   var tt pp;

run;
Thank you so much in advance.





 
In this case I think you've gotten a bit mixed up as there is no macro here?

A macro is generally done for code efficiency where something that is coded multiple times and writing a macro saves the coder time. Macros generally do not make your code process faster.

If all you want is to amend the code to use infile rather than datalines then..


option ls=78;

data xx;
infile C:\problem1.txt dsd missover dlm=' '; /*This is the location of your text file.
dsd and missover are infile options, further reading for you
dlm is the delimeter of the read in file*/
input y1-y12;
array ss(*) y1-y12;
m=4; nn=12;

do n1=1 to nn-m+1;
do n2=n1+1 to nn-m+2;
do n3=n2+1 to nn-m+3;
do n4=n3+1 to nn-m+4;
do n5=n4+1 to nn-m+5;

sum=ss(n1)+ss(n2)+ss(n3)+ss(n4)+ss(n5);
if sum <=0 then t=1; else t=0;
output;
end; end; end; end; end;


run;

/* The main objective is instead of using datalines here, store the data in a file and use infile keyword */

proc univariate noprint;
var t;
output out=temp n=nn sum=tt;

data xx;
set temp;
pp=tt*2/nn;
proc print data=xx;
var tt pp;

run;
 
Please help with this SAS code.
Below is the question that describes in detail.

file1.txt X 4 3 1 2 7 5 5
file2.txt Y 20 7 8 5 2 4 15

X-Y= -16 -4 -7 -3 5 1 -10

Write a macro for the following SAS code and store it so that it can be used to run on different data sets.
1. Read the data
2. Find the difference
3. Create a subfile that has only positive difference
4. Find the sum


The SAS Code
dm log 'clear';
dm output 'clear';
options ls=78;

data xx;
input y1-y7;
array ss(*) y1-y7;
array x(*) y1-y7;

do n1=1 to 2; if n1=1 then x(1)=ss(1); else x(1)=0;
do n2=1 to 2; if n2=1 then x(2)=ss(2); else x(2)=0;
do n3=1 to 2; if n3=1 then x(3)=ss(1); else x(3)=0;
do n4=1 to 2; if n4=1 then x(4)=ss(1); else x(4)=0;
do n5=1 to 2; if n5=1 then x(5)=ss(1); else x(5)=0;
do n6=1 to 2; if n6=1 then x(6)=ss(1); else x(6)=0;
do n7=1 to 2; if n7=1 then x(7)=ss(1); else x(7)=0;

sum=x(1)+x(2)+x(3)+x(4)+x(5)+x(6)+x(7);
if sum<=6 then t=1; else t=0;
output;
end;end;end;end;end;end;end;

datalines;
16 4 7 3 5 1 10
run;

proc univariate noprint;
var t;
output out=temp n=nn sum=tt;
run;

data xx;
set temp;
pp=tt*2/nn;
run;


proc print data=xx;
var tt pp;
run;


Thank you in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top