BookmarkSubscribeRSS Feed
SASMom2
Fluorite | Level 6

I have a dataset that has two variables. The first columns has string variable called savings and another column has dollar amount of savings. There are three types of savings. So, basically, there are three rows and two columns. I use this table to create an excel file using ODS tagset.

 

I do not want to create an excel file if there was no dollar amount in ALL three savings I second column.

 

SavingsSaving_Amount
ABC 
DEF 
GHI 

 

 

How do I program this?

 

Thanks for your help.

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Hi @SASMom2   You could use a conditional macro IF THEN statement

 

proc sql noprint;
select nmiss(Saving_Amount) ne count(*) into :check trimmed
from have;
quit;

%put &=check;

%if  &check %then %put creat excel file;

I hope you get the idea

ed_sas_member
Meteorite | Level 14

Hi @SASMom2 

 

You can use a macro to run your code conditionally:

data mydata;
	input Savings $	Saving_Amount;
	cards;
ABC .
DEF .
GHI .
;
run;


%macro _savings ();
	proc sql noprint;
		select count(*) into:mis_amount from mydata where Saving_Amount=.;
	quit;
	%if &mis_amount = 3 %then %do;
		proc print;/*<ODS TAGSETS statement>*/
	%end;
%mend;

%_savings
SASMom2
Fluorite | Level 6

Thank you for your reply. I think I am almost there except I want to say below.

 

%macro _savings ();
proc sql noprint;
select count(*) into:mis_amount from mydata where Saving_Amount=.;
quit;
%if &mis_amount = 0 %then %do;

got to the end of the code and exit.

%end;
%mend;
%_savings

SASMom2
Fluorite | Level 6

thank you both for your reply. It worked!

s_lassen
Meteorite | Level 14

Another possibility is to skip the macro stuff, and write to a temporary file instead:

filename tempsas temp;
set have;
  where saving_amount is not null;
  file tempsas:
  put 'Proc Print data=have;run;';
  stop;
run;

%include tempsas;

That way, if there are no non-missing values, nothing will be written to your temporary SAS file, and nothing will be executed with the %INCLUDE statement.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 2396 views
  • 0 likes
  • 4 in conversation