Help using Base SAS procedures

displaying proc means

Reply
Occasional Contributor
Posts: 13

displaying proc means

Hello community,

The following code does not include personal information of individuals. In the following code, I am trying to do simple descriptive statistics using proc means of 6 continuous variables with a previously established outcome variable, "bothstatus".

Bothstatus has 4 outcome levels, these reflect HIV and HSV status of npnidentifiable persons:

0= "HIV-, HSV-"

1= "HIV-,HSV+"

2="HIV+, HSV-"

3= "HIV+, HSV+"

With the following code, I'm trying to first display descriptive statistics per level of this outcome variable, "bothstatus", this is displayed by each proc means procedure for all 6 continuous variables. The proc means is arranged in such a way, so that the mean, standard deviation and number of observations can be displayed in the output for each variable. However, I would like some assistance as to how to display missing for each variable, hopefully in the same table. I have tried coding number missing as "nmiss" in the proc means line, I have also tried coding it similar to std, mean, and number of observations, but it does not display any missing. Any assistance with this part will be great.

The next section of code, attempts to merge the 4 output datasets created in the 4 proc means statements. This merge, with its respective proc print procedure is done for all 6 continuous variables. In this section, I was needing help as to how to display the different variables shown in proc print with a different output label... such as the variable n0age can display: "number of observations: HIV-, HSV-", another example, n1age can display "HIV-,HSV+"

I believe this code can also be less cumbersome if I change the coding of the outcome variable, bothstatus, can be recoded to a character variable. However I do not know well how to define values with labels, the code is as follows:

data NewVar2;

set psw.NewVar1;

data hivhsvneg ;

set NewVar2 ;

if bothstatus=0 ;

data hsvpos ;

set NewVar2  ;

if bothstatus=1 ;

data hivpos ;

set NewVar2  ;

if bothstatus=2 ;

data hivhsvpos ;

set NewVar2  ;

if bothstatus=3 ;

*Means procedure for each level of the outcome variable;

* new method to develop proc means for each level;

title 'Descriptive statistics: HIV-, HSV-';

proc means data=hivhsvneg;

var age yrssch sex_1st fstepa fcaspa flifpa2;

output out=hivhsvneg       n=n0age n0yrssch n0sex_1st n0fstepa n0fcaspa n0flifpa2

mean=mean0age mean0yrssch mean0sex_1st mean0fstepa mean0fcaspa mean0flifpa2

std= std0age std0yrssch std0sex_1st std0fstepa std0fcaspa std0flifpa2;

run;

*nmiss=nmiss0age nmiss0yrssch nmiss0sex_1st nmiss0fstepa nmiss0fcaspa nmiss0flifpa2;

title 'Descriptive statistics: HIV-, HSV+';

proc means data=hsvpos;

var age yrssch sex_1st fstepa fcaspa flifpa2;

output out=hsvpos       n=n1age n1yrssch n1sex_1st n1fstepa n1fcaspa n1flifpa2

mean=mean1age mean1yrssch mean1sex_1st mean1fstepa mean1fcaspa mean1flifpa2

std= std1age std1yrssch std1sex_1st std1fstepa std1fcaspa std1flifpa2;

run;

title 'Descriptive statistics: HIV+, HSV-';

proc means data=hivpos;

var age yrssch sex_1st fstepa fcaspa flifpa2;

output out=hivpos       n=n2age n2yrssch n2sex_1st n2fstepa n2fcaspa n2flifpa2

mean=mean2age mean2yrssch mean2sex_1st mean2fstepa mean2fcaspa mean2flifpa2

std= std2age std2yrssch std2sex_1st std2fstepa std2fcaspa std2flifpa2;

run;

title 'Descriptive statistics: HIV+, HSV+';

proc means data=hivhsvpos;

var age yrssch sex_1st fstepa fcaspa flifpa2;

output out=hivhsvpos n=n3age n3yrssch n3sex_1st n3fstepa n3fcaspa n3flifpa2

mean=mean3age mean3yrssch mean3sex_1st mean3fstepa mean3fcaspa mean3flifpa2

std= std3age std3yrssch std3sex_1st std3fstepa std3fcaspa std3flifpa2;

run;

ods pdf close;

run;

** Merging of datasets output for each continuous variable;

* age merge output;

data age ;

merge hivhsvneg hsvpos hivpos hivhsvpos ;

proc print data=age ;

var n0age n1age n2age n3age mean0age mean1age mean2age mean3age std0age std1age std2age std3age;

run;

*yrssch merge output;

data yrssch ;

merge hivhsvneg hsvpos hivpos hivhsvpos ;

proc print data=yrssch ;

var n0yrssch n1yrssch n2yrssch n3yrssch mean0yrssch mean1yrssch mean2yrssch mean3yrssch std0yrssch std1yrssch std2yrssch std3yrssch ;

run ;

*sex_1st merge output;

data firstsex ;

merge hivhsvneg hsvpos hivpos hivhsvpos;

proc print data=firstsex ;

var n0sex_1st n1sex_1st n2sex_1st n3sex_1st mean0sex_1st mean1sex_1st mean2sex_1st mean3sex_1st std0sex_1st std1sex_1st std2sex_1st std3sex_1st ;

run ;

*fstepa merge output;

data fstepa ;

merge hivhsvneg hsvpos hivpos hivhsvpos;

proc print data=fstepa ;

var n0fstepa n1fstepa n2fstepa fstepa mean0fstepa mean1fstepa mean2fstepa mean3fstepa std0fstepa std1fstepa std2fstepa std3fstepa;

run ;

*fcaspa merge output;

data fcaspa ;

merge hivhsvneg hsvpos hivpos hivhsvpos;

proc print data=fcaspa ;

var n0fcaspa n1fcaspa n2fcaspa n3fcaspa mean0fcaspa mean1fcaspa mean2fcaspa mean3fcaspa std0fcaspa std1fcaspa std2fcaspa std3fcaspa ;

run ;

*flifpa2 merge output;

data flifpa2 ;

merge hivhsvneg hsvpos hivpos hivhsvpos;

proc print data=flifpa2 ;

var n0flifpa2 n1flifpa2 n2flifpa2 n3flifpa2 mean0flifpa2 mean1flifpa2 mean2flifpa2 mean3flifpa2 std0flifpa2 std1flifpa2 std2flifpa2 std3flifpa2 ;

run ;

Thank you so much!

Super User
Super User
Posts: 7,401

Re: displaying proc means

Hi,

Some tips.  Firstly, follow some good programming practices, indent within groups, end statements with run; or quit;  Give some test data and required output.  Minimise the question, the code is too long and without anything to run it on, am not going to spend the time trying to figure out what its doing.

Secondly, not sure why you are breaking your dataset up and running means on each part, use a by statement or a class statement (they have slightly different resulting rows) in the means. 

Thirdly, for missings there are a few ways to do it.  Me personally I create a template dataset which has all the required outputs rows, this I then set with the means result to ensure all categories are present

Super User
Posts: 10,497

Re: displaying proc means

See if this is helpful:

proc format;

value bothstatus

0= "HIV-, HSV-"

1= "HIV-,HSV+"

2="HIV+, HSV-"

3= "HIV+, HSV+"

;

run;

proc tabulate data=psw.NewVar1;

     class bothstatus / preloadfmt;

     format bothstatus bothstatus.;

     var  age yrssch sex_1st fstepa fcaspa flifpa2;

     table bothstatus * ( age yrssch sex_1st fstepa fcaspa flifpa2),

              n mean std

              / printmiss ;

run;

Ask a Question
Discussion stats
  • 2 replies
  • 188 views
  • 3 likes
  • 3 in conversation