DATA Step, Macro, Functions and more

Running a macro from _N_=1 to end of file

Accepted Solution Solved
Reply
Super Contributor
Posts: 503
Accepted Solution

Running a macro from _N_=1 to end of file

Hi Everyone,

I have 2 files: entry file and Master file.

I want to take subsample from Master file that meet each condition in entry file to further analyze.

(it is a bit like proc SQL to get subsample but the analysis is complicate that I can create that big file using proc SQL).

 

So my solution is that:

Step 1: start from entry file, work with the 1st record, a and 1.

Step 2: I go to mater file and select all records that meet condition in step 1, or records with a =1 --> 2 record

      analyze that subsample.

      after I am done

Go back to step 1: work on 2nd record

 

 

I vision my code has 2 macro;

      Outer macro that run throught entry file

      Inner macro that pick record from master and process

 

I want to run from the beginning to the end of the entry file but I dont know how to do

it.

Could you please help me?

 

Thank you so much for your help.

 

HC

 


data entry;
input variable $ value;
datalines;
a 1
a 2
b 6
b 9
;run;

data masterfile; 
input a b c d;
datalines;
 1 2 3 4
 1 9 3 2
 2 9 2 3
;
run;


	*the first record in entry file;
	data _entry_temp; set entry;
	if _N_=1;
	run;

	*create macro value to feed the inner macro;
	proc sql noprint;	select variable into:   entry_name 	from _entry_temp; quit;
	proc sql noprint;	select value into : 	entry_value 	from _entry_temp; quit;

	*Use the above to 2 value to feed inner Macro in a seperate file
	for checking purpose, I just want to create new file with macro name;
		%macro inner(entry_name2 = ,entry_value2=);
			data want_&entry_name.&entry_value; set _entry_temp;
			run;
		%mend;

Accepted Solutions
Solution
‎02-01-2018 05:33 PM
Super Contributor
Posts: 503

Re: Running a macro from _N_=1 to end of file

oh, I got it. Not perfect but get the job done.

 

a quick question: why this one doesnt work?

 

data want_&entry_name&entry_value; set _entry_temp;run;

 

data entry;
input variable $ value;
datalines;
a 1
a 2
b 6
b 9
;run;

proc sql noprint;
select count(*) into: nobs from entry;quit;

%put &nobs;

data masterfile; 
input a b c d;
datalines;
 1 2 3 4
 1 9 3 2
 2 9 2 3
;
run;


%MACRO SINGLE_FACTOR;
%DO ENTRY_COUNT=1 %TO &NOBS;
%PUT "NEW ROUND";
%put &ENTRY_COUNT;
	*the first record in entry file;
	data _entry_temp; set entry;
	if _N_=&ENTRY_COUNT;
	run;

	*create macro value to feed the inner macro;
	proc sql noprint;	select variable into:   entry_name 		from _entry_temp; quit;
	proc sql noprint;	select value into 	: 	entry_value 	from _entry_temp; quit;

	%put &entry_name;run; 
	%put &entry_value;run; 

	*Use the above to 2 value to feed inner Macro in a seperate file
		for checking purpose, I just want to create new file with macro name;
			data want; set masterfile;
			IF &entry_name=&entry_value;
			run;

%END;
%MEND;

%SINGLE_FACTOR;

View solution in original post


All Replies
Solution
‎02-01-2018 05:33 PM
Super Contributor
Posts: 503

Re: Running a macro from _N_=1 to end of file

oh, I got it. Not perfect but get the job done.

 

a quick question: why this one doesnt work?

 

data want_&entry_name&entry_value; set _entry_temp;run;

 

data entry;
input variable $ value;
datalines;
a 1
a 2
b 6
b 9
;run;

proc sql noprint;
select count(*) into: nobs from entry;quit;

%put &nobs;

data masterfile; 
input a b c d;
datalines;
 1 2 3 4
 1 9 3 2
 2 9 2 3
;
run;


%MACRO SINGLE_FACTOR;
%DO ENTRY_COUNT=1 %TO &NOBS;
%PUT "NEW ROUND";
%put &ENTRY_COUNT;
	*the first record in entry file;
	data _entry_temp; set entry;
	if _N_=&ENTRY_COUNT;
	run;

	*create macro value to feed the inner macro;
	proc sql noprint;	select variable into:   entry_name 		from _entry_temp; quit;
	proc sql noprint;	select value into 	: 	entry_value 	from _entry_temp; quit;

	%put &entry_name;run; 
	%put &entry_value;run; 

	*Use the above to 2 value to feed inner Macro in a seperate file
		for checking purpose, I just want to create new file with macro name;
			data want; set masterfile;
			IF &entry_name=&entry_value;
			run;

%END;
%MEND;

%SINGLE_FACTOR;
☑ This topic is solved.

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

Discussion stats
  • 1 reply
  • 95 views
  • 0 likes
  • 1 in conversation