<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to conditionally output another dataset when the data set you outputted has 0 obs? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-output-another-dataset-when-the-data-set/m-p/812374#M320527</link>
    <description>This was really great. Thanks for your help!</description>
    <pubDate>Tue, 10 May 2022 12:19:30 GMT</pubDate>
    <dc:creator>Hello_there</dc:creator>
    <dc:date>2022-05-10T12:19:30Z</dc:date>
    <item>
      <title>How to conditionally output another dataset when the data set you outputted has 0 obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-output-another-dataset-when-the-data-set/m-p/811342#M319996</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
infile datalines dsd dlm=",";
	input subj $ treatment $ painlvl bodypart $;
datalines;
001, A, 2, leg
002, B, 1, head
003, A, 1, arm
004, B, 1, eye
005, B, 2, mouth
; 
run; * choose distinct subjects with highest severity (if more than one instance);

/* 
2 treatment groups (A or B)
3 levels of pain, 1=low, 2=medium, 3=high
*/

%macro howMany(pain, dsnout);
proc freq data=have1 noprint;
where painlvl=&amp;amp;pain;
	table treatment/ out=count;
run;

data a; set count;
	count_percent=strip(put(count,32.))||" (" || strip(put(percent, 32.2))||")";
run;
proc transpose data=a out=&amp;amp;dsnout (drop= _name_);
	id treatment;
	var count_percent;
run;

data _empty; * if the data set is empty, how do i output this?;
infile datalines dsd dlm=",";
	input a $ b $;
datalines;
0, 0
;
run;


%mend howMANY;

%howMANY(1, _rowa);
%howMANY(2, _rowb);
%howMANY(3, _rowc);

data finished; set _row:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;P&gt;* n=5&lt;/P&gt;
&lt;P&gt;* 2 treatment groups (A or B)&lt;/P&gt;
&lt;P&gt;* 3 levels of pain (1=low, 2= medium, 3=high)&lt;/P&gt;
&lt;P&gt;* goal: There are no instances of anyone having high pain (painlvl=3). I've created a data set inside the macro that I would like to use that shows 0's for the two variables that will concatenated onto the first two rows that have counts and percents (1=low, 2=medium).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I could use proc summary and use the preloadfmt option to include 0's. The issue is that I have to manually merge a denominator for the whole population which the n=5 is just a subset of.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have relatively novice experience w/ SAS. And I was wondering if I could I could do something like an %if, % then do thing in a macro. But I don't have experience w/ that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 03 May 2022 23:14:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-output-another-dataset-when-the-data-set/m-p/811342#M319996</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-05-03T23:14:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally output another dataset when the data set you outputted has 0 obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-output-another-dataset-when-the-data-set/m-p/812373#M320526</link>
      <description>&lt;P&gt;Here's one way to work around this&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
infile datalines dsd dlm=",";
	input subj $ treatment $ painlvl bodypart $;
datalines;
001, A, 2, leg
002, B, 1, head
003, A, 1, arm
004, B, 1, eye
005, B, 2, mouth
; 
run; * choose distinct subjects with highest severity (if more than one instance);

/* 
2 treatment groups (A or B)
3 levels of pain, 1=low, 2=medium, 3=high
*/

%macro howMany(pain, dsnout);
proc freq data=have1 noprint;
where painlvl=&amp;amp;pain;
	table treatment/ out=count;
run;

data a; 
	set count ;
	count_percent=strip(put(count,32.))||" (" || strip(put(percent, 32.2))||")";
run;


proc transpose data=a out=&amp;amp;dsnout (drop= _name_);
	id treatment;
	var count_percent;
run;

/* Set macro variable emptyDS to N */
/* Assuming the dataset &amp;amp;dsnout has observations at this point */
%let emptyDS=N ;

/* Datastep checks if &amp;amp;dsnout has observations using the ATTRN function */
/* If it doesn't have observations then flip macro varible emptyDS to Y */
data _null_ ;
	dsid=open("&amp;amp;dsnout") ;
	obsOrVars=attrn(dsid,"ANY") ;
	if obsOrVars ne 1 then
		call symput("emptyDS","Y") ;
	dsid=close(dsid) ;
run ;
	
/* Check value of macro variable emptyDS */
/* If it is Y (an empty dataset) then    */
/* create dataset with observations      */
%if &amp;amp;emptyDS=Y %then %do ;
	data &amp;amp;dsnout ;
		a="0" ;
		b="0" ;
		output ;
	run ;	
%end ;
/*
data _empty; * if the data set is empty, how do i output this?;
infile datalines dsd dlm=",";
	input a $ b $;
datalines;
0, 0
;
run;
*/

%mend howMANY;

%howMANY(1, _rowa);
%howMANY(2, _rowb);
%howMANY(3, _rowc);

data finished; set _row:;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 May 2022 12:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-output-another-dataset-when-the-data-set/m-p/812373#M320526</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2022-05-10T12:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to conditionally output another dataset when the data set you outputted has 0 obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-output-another-dataset-when-the-data-set/m-p/812374#M320527</link>
      <description>This was really great. Thanks for your help!</description>
      <pubDate>Tue, 10 May 2022 12:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-conditionally-output-another-dataset-when-the-data-set/m-p/812374#M320527</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-05-10T12:19:30Z</dc:date>
    </item>
  </channel>
</rss>

