Date: Sun, 25 Jan 2004 17:10:25 -0500
Reply-To: Don Stanley <don_stanley@PARADISE.NET.NZ>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Don Stanley <don_stanley@PARADISE.NET.NZ>
Subject: Re: help: count non-missing values?
Assuming Bills assumptions, and looking to answer his question about how to
streamline the solution that he presented, the following reduces this
problem to just one simple datastep. Note that using $char as the input
format preserves the '.' in the input string which avoids Bill's concerns
with valid blanks in the input stream.
As Bill has noted, this may or may not have answered the originally posted
question which is a little unclear.
data a;
informat one two three $char1. ;
input ONE TWO THREE ;
miss = 0 ; nonmiss = 0 ;
array charvars one -- three ;
do i=1 to dim(charvars) ;
if charvars(i) eq '.' then miss + 1 ;
else nonmiss + 1 ;
end ;
put (_all_) (=);
cards4;
A A .
. B C
E . .
;;;;
run;
Cheers
Don
On Sun, 25 Jan 2004 07:59:27 -0800, Bill McKirgan maxsfolks <bill-
mckirgan@UIOWA.EDU> wrote:
>peppermint_jojo@YAHOO.COM (Yuan C.) wrote in message
news:<200401240603.i0O63hS10346@listserv.cc.uga.edu>...
>> Dear SAS-L,
>> Can someone give a little hint on this problem? I want to generate a
>> column with values equal to the number of non-missing values of variables
>> ONE, TWO, and THREE. Thanks alot.
>>
>> ONE TWO THREE
>> A A .
>> . B C
>> E . .
>
>Yuan,
>
>If we assume variables ONE TWO and THREE are character *and*
>that '.' is used to signify missing (instead of a character space);
>then, you can do something like the following to obtain your
>counts.
>
>Best regards,
>Bill McKirgan
>
>PS: There's got to be more elequent ways of doing this...arrays?
>...proc freq and output to a dataset? Post more details (as others
>have requested) and you
>may get some better solutions.
>
>
>-----program---------
>
>
>data a;
>input ONE $ TWO $ THREE $;
>cards4;
> A A .
> . B C
> E . .
>;;;;
>run;
>
>
>data b; set a;
>put (_all_) (=);
>run;
>
>/* NOTICE the '.' was interepreted as missing and set to ' '
>
>ONE=A TWO=A THREE=
>ONE= TWO=B THREE=C
>ONE=E TWO= THREE=
>
>*/
>
>/* to restore the '.' (assuming all blanks at this point were
>formerly '.' */
>data c; set b;
> if one='' then one='.';
> if two='' then two='.';
> if three='' then three='.';
>put (_all_) (=);
>run;
>
>/*
>ONE=A TWO=A THREE=.
>ONE=. TWO=B THREE=C
>ONE=E TWO=. THREE=.
>*/
>
>
>data report; set c;
>/* initialize counters and label */
> nm_count=0; label nm_count='non-missing count';
> m_count=0; label m_count='missing count';
>/* index for "missing" */
> mA = index(one,".");
> mB = index(two,".");
> mC = index(three,".");
>/* count all nonmissing based on what *was found* to be missing*/
> IF mA=0 then nmA=1; ELSE NMA=0;
> IF mB=0 then nmB=1; ELSE NMB=0;
> IF mC=0 then nmC=1; ELSE NMC=0;
>/* sum things up */
> NM_COUNT = SUM ( OF NMA NMB NMC );
> M_COUNT = SUM ( OF MA MB MC );
>/* printing the results to the log */
> if _n_=1 then do;
> put "VARS | missing | nonmiss | COUNTS "; end;
> PUT
> ONE two three " | "
> mA mB mC " | "
> nmA nmB nmC " | "
> M_COUNT NM_COUNT
> ;
>RUN;
>
>/*
>VARS | missing | nonmiss | COUNTS
>A A . | 0 0 1 | 1 1 0 | 1 2
>. B C | 1 0 0 | 0 1 1 | 1 2
>E . . | 0 1 1 | 1 0 0 | 2 1
>*/
>
>options nocenter nodate nonumber;
>proc print l;
>var one two three nm_count m_count;
>sum nm_count m_count;
>run;
>
>--------proc print output----------
> non-missing missing
>Obs ONE TWO THREE count count
>
> 1 A A . 2 1
> 2 . B C 2 1
> 3 E . . 1 2
> =========== =======
> 5 4