BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26
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.

hhchenfx
Barite | Level 11

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

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

Astounding
PROC Star

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

Kurt_Bremser
Super User

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.

srinath3111
Quartz | Level 8

%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;

Satish_Parida
Lapis Lazuli | Level 10

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;
Reeza
Super User

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;
hhchenfx
Barite | Level 11

WOW that's great.

Thanks a lot.

HC

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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