DATA Step, Macro, Functions and more

Skip macro iteration if data file less than certain records

Accepted Solution Solved
Reply
Super Contributor
Posts: 503
Accepted Solution

Skip macro iteration if data file less than certain records

[ Edited ]

Hi Everyone,

 

In each iteration, my macro will create a new file and run the analysis.

However, I dont want to run the analysis for a new file with less than 20 records.

 

Can you please fix my code below?

 

Many thanks.

 

HC

 

%Macro running_over_all_data(var_name = , value=);
	
	data _TEMP; set original_file;
	keep date tradetim	;
	     IF &var_name=&value;
	run;
	
			*if too small number of record --> no running code;
			data _NULL_;
				call symput('nobs',put(x,best.));
				set _TEMP nobs=x;
				stop; 
				put nobs;

	%IF &NOBS > 5 %THEN %DO;
	        %include "C:\Users\Main analysis code.sas" /lrecl=50000;
	%END;
%Mend;


Accepted Solutions
Solution
‎01-31-2018 02:52 PM
Super User
Posts: 22,822

Re: Skip Marco iteration if datafile less than certain records

Posted in reply to Satish_Parida

Another version - SQL creates the count automatically so that's a change and then if you're running this multiple times you should clean up the _temp data set so the next time you run it you aren't hitting an old data set. 

 


%Macro running_over_all_data(var_name = , value=);
	
    proc sql;
    create table _temp as
    select date, tradetim
    from original_file
    where &var_name=&value;
    quit;

    %let nobs=&sqlobs;


	%IF &NOBS > 5 %THEN %DO;
	        %include "C:\Users\Main analysis code.sas" /lrecl=50000;
	%END;

    %*always clean up when you Are going to run a macro multiple times;
    proc sql noprint;
    drop table _temp;
    quit;

%Mend;

View solution in original post


All Replies
Super User
Super User
Posts: 9,193

Re: Skip Marco iteration if datafile less than certain records

data _null_;
  set sashelp.vtable (where=(libname="WORK" and memname="ORIGINAL_FILE" and nobs > 20));
  call execute('%include "c:/users/main analysis code.sas" / lrecl=5000;');
run;

No need for all that code, the set will only create an observation if more than 20 records are present, if there is no observation then the call execute does not get generated.  Simple.

Super Contributor
Posts: 503

Re: Skip Marco iteration if datafile less than certain records

In my case, I might have to SQL inside Macro to get the file.

 

Super User
Super User
Posts: 9,193

Re: Skip Marco iteration if datafile less than certain records

You can put any code you like before this, SQL, SAS anything.  I don't see what that has to do with the question you asked?

Super User
Posts: 6,534

Re: Skip Marco iteration if datafile less than certain records

Your DATA _NULL_ step needs a RUN statement.  Without it, macro language examines &NOBS before executing the DATA step.

Super User
Posts: 9,551

Re: Skip Marco iteration if datafile less than certain records

Since your data step has not yet run when the %if condition is evaluated (it will start to run once a step boundary is detected, which happens at the first proc or data stament in the included file), &nobs does not exist (or it could exist in the global symbol table with a wrong value).

Add a run statement before your %if.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 37

Re: Skip Marco iteration if datafile less than certain records

[ Edited ]

%Macro running_over_all_data(var_name = , value=);

data  _TEMP;

set original_file;

keep date tradetim ;

%IF &var_name=&value;

run;

 

*if too small number of record --> no running code;

 

 

data _NULL_;

%do i=1 %to nobs;

set _TEMP ;

%if &i ge 20 then do;

%include "C:\Users\Main analysis code.sas" /lrecl=50000;

%END;

%Mend;

Super User
Posts: 9,551

Re: Skip Marco iteration if datafile less than certain records

Posted in reply to srinath3111

Wow.

data _NULL_;

set _TEMP (where=(_N_>=20) then  Do;

If you ever get that to run, you're McGyver.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 109

Re: Skip Marco iteration if datafile less than certain records

Corrections to your macro: Hope it helps.

 

 

%Macro running_over_all_data(var_name = , value=);
	
	data _TEMP; set original_file;
	keep date tradetim	;
	     IF &var_name=&value;
	run;                                                *Not Touching your Data step;
	
			*if too small number of record --> no running code;
			Proc Sql noprint;
                        select count(*) into :NOBS from _TEMPl;
                        Quit;                *Taking count of the whole table;

	%IF &NOBS > 5 %THEN %DO;
	        %include "C:\Users\Main analysis code.sas" /lrecl=50000;
	%END;
%Mend;
Solution
‎01-31-2018 02:52 PM
Super User
Posts: 22,822

Re: Skip Marco iteration if datafile less than certain records

Posted in reply to Satish_Parida

Another version - SQL creates the count automatically so that's a change and then if you're running this multiple times you should clean up the _temp data set so the next time you run it you aren't hitting an old data set. 

 


%Macro running_over_all_data(var_name = , value=);
	
    proc sql;
    create table _temp as
    select date, tradetim
    from original_file
    where &var_name=&value;
    quit;

    %let nobs=&sqlobs;


	%IF &NOBS > 5 %THEN %DO;
	        %include "C:\Users\Main analysis code.sas" /lrecl=50000;
	%END;

    %*always clean up when you Are going to run a macro multiple times;
    proc sql noprint;
    drop table _temp;
    quit;

%Mend;
Super Contributor
Posts: 503

Re: Skip Marco iteration if datafile less than certain records

WOW that's great.

Thanks a lot.

HC

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 140 views
  • 4 likes
  • 7 in conversation