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=&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=&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;
Hi,
Notes:
* n=5
* 2 treatment groups (A or B)
* 3 levels of pain (1=low, 2= medium, 3=high)
* 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).
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.
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.
Thanks
Here's one way to work around this
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=&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=&dsnout (drop= _name_);
id treatment;
var count_percent;
run;
/* Set macro variable emptyDS to N */
/* Assuming the dataset &dsnout has observations at this point */
%let emptyDS=N ;
/* Datastep checks if &dsnout has observations using the ATTRN function */
/* If it doesn't have observations then flip macro varible emptyDS to Y */
data _null_ ;
dsid=open("&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 &emptyDS=Y %then %do ;
data &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;
Here's one way to work around this
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=&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=&dsnout (drop= _name_);
id treatment;
var count_percent;
run;
/* Set macro variable emptyDS to N */
/* Assuming the dataset &dsnout has observations at this point */
%let emptyDS=N ;
/* Datastep checks if &dsnout has observations using the ATTRN function */
/* If it doesn't have observations then flip macro varible emptyDS to Y */
data _null_ ;
dsid=open("&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 &emptyDS=Y %then %do ;
data &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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.