BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Noleen
Calcite | Level 5

I need to count how many numbers starting with '1' '2' '3's in each observation.

There are 700 variables and 50000 observations.

I use the following code,

data want;

set have;

array chars $ chars1-char700;

array counts counts1-counts3;

do over chars ;

if substr(chars,1,1)=1 then counts1+1;

if substr(chars,1,1)=2 then counts2+1;

if substr(chars,1,1)=3 then counts3+1;

end;

run;

but the counts on each observation is adding on top of the result (counts) from the last observation.

How can I count them for each observation? (the counts need to star from 0 for each observation)

Many thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

data want;

set have;

array chars $ chars1-char700;

array counts counts1-counts3;

counts1=0;counts2=0;counts3=0;

do over chars ;

if substr(chars,1,1)=1 then counts1+1;

if substr(chars,1,1)=2 then counts2+1;

if substr(chars,1,1)=3 then counts3+1;

end;

run;

View solution in original post

7 REPLIES 7
Reeza
Super User

Do you only have 1,2,3?

To answer your initial question counts1+1 creates an implicit retain, change the counter to counts1=counts1+1 or explicitly set the variables to 0 at the beginning of each loop

Noleen
Calcite | Level 5

Thanks Reeza,

I tried count1=count1+1, it doesn't count the number at all. I don't know why. Do I need to set count1=0 at the beginning?

BTW, how can I set counts back to 0 at the beginning of the loop?

Cheers

Haikuo
Onyx | Level 15

On top of Reeza's suggestions, you can also set counts missing for every data step iteration:

call missing (of counts(*)); Some simplification has also been suggested in the following code:

data want;

     set have;

     array chars chars1-chars700;

     array counts counts1-counts3;

     call missing(of counts(*));

     do over chars;

           counts(input(first(chars),1.))+1;

     end;

run;

Noleen
Calcite | Level 5

Thanks Hai,

I'm not sure I understand it.

Is the count missing can reset variables to 0 ?

Cheers,

Reeza
Super User

data want;

set have;

array chars $ chars1-char700;

array counts counts1-counts3;

counts1=0;counts2=0;counts3=0;

do over chars ;

if substr(chars,1,1)=1 then counts1+1;

if substr(chars,1,1)=2 then counts2+1;

if substr(chars,1,1)=3 then counts3+1;

end;

run;

Noleen
Calcite | Level 5

Hi Reeza;

It still sum up the counts..

sas.png

Noleen
Calcite | Level 5

Hi Reeza,

I miss typed the name.

It works!! Thanks a lot!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2070 views
  • 1 like
  • 3 in conversation