```Date: Fri, 27 Sep 2002 01:16:22 +0800 Reply-To: fred Sender: "SAS(r) Discussion" From: fred Organization: xk Subject: Re: Individual count for each Record Richard it seems that the retain statement is redundant in this prog. It has been commented out but the prog works fine. Please advise. Btw, why is the 'input text \$' created variable - text - restricted to 8 characters? TIA. data set1 ; input snum \$ text \$ ; cards; 1 11XXY11222335 2 124YYZ4400994 3 34X88Z23Z3331 4 237X772Y32Z36 5 5ZZ3Y46080827 ; data set2 (drop=i); set set1; by snum; *retain n0 0 n1 0 n2 0 n3 0 n4 0 n5 0 n6 0 n7 0 n8 0 n9 0 A24 0 A25 0 A26 0; if first.snum then do; n0=0; n1=0; n2=0; n3=0; n4=0; n5=0; n6=0; n7=0; n8=0; n9=0; A24=0; A25=0; A26=0; end; do i=1 to length(text); if substr(text,i,1)='0' then n0=n0+1; if substr(text,i,1)='1' then n1=n1+1; if substr(text,i,1)='2' then n2=n2+1; if substr(text,i,1)='3' then n3=n3+1; if substr(text,i,1)='4' then n4=n4+1; if substr(text,i,1)='5' then n5=n5+1; if substr(text,i,1)='6' then n6=n6+1; if substr(text,i,1)='7' then n7=n7+1; if substr(text,i,1)='8' then n8=n8+1; if substr(text,i,1)='9' then n9=n9+1; if substr(text,i,1)='X' then a24=a24+1; if substr(text,i,1)='Y' then a25=a25+1; if substr(text,i,1)='Z' then a26=a26+1; end; if last.snum then output; run; proc print noobs; run; "Richard A. DeVenezia" wrote in message news:Bw0k9.1249\$Xg1.667@tornadotest1.news.pas.earthlink.net... > > > Retain maintains a variables values on subsequent records (default > behaviour is to set all PDV variables to missing at the top of the implied > data loop). You would need to reset the retain variables to zero before > (or after) using them. > > Here another solution: > > data digitCounts; > set set1; > array n_[0:9] n0-n9; * automatically reset to missing for each obs.; > do i = 1 to length (text); > > * assume charset has digit characters which are monotonic increasing by > one; > * e.g. ascii '0' = '30'x, '1' = '31'x, ... '9' = '39'x; > * I believe ebcdic is also mono+1, not sure about asian or double-byte; > > d = rank(substr(text,i,1)) - rank('0'); > > if 0 <= d <= 9 then > n_[d] = sum(n_[d],1); * sum treats missing like a zero; > end; > output; > run; > > -- > Richard A. DeVenezia > http://www.devenezia.com/downloads/sas/macros/#sas2xls > Use Perl to create an Excel file containing one worksheet per SAS dataset > > ```

Back to: Top of message | Previous page | Main SAS-L page