Count across variables (substring)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Count across variables (substring)

Hi,

I'd like to count the number of a specific value ('4') per individual in the second digit of a character variable (one per day) and store the count (new variable). The data set:

pid    date1   date2   date3  ...

1       142      121     141

2       111       12B    121

3       141       14A    121

 

Anyone? 

 


Accepted Solutions
Solution
‎04-05-2017 05:12 AM
Occasional Contributor
Posts: 6

Re: Count across variables (substring)


All Replies
Trusted Advisor
Posts: 1,137

Re: Count across variables (substring)

Hope its what you are expecting

 

data have;
input pid    date1$   date2$   date3$;
array cnt(3) cnt1-cnt3;
array dat(3) date1-date3;
do i = 1 to 3;
if substr(dat(i),2,1)='4' then cnt(i)=1;
end;
count=sum(of cnt1-cnt3);
cards;
1       142      121     141
2       111       12B    121
3       141       14A    121
;

 

Thanks,
Jag
Solution
‎04-05-2017 05:12 AM
Occasional Contributor
Posts: 6

Re: Count across variables (substring)

Thanks!
Super User
Super User
Posts: 7,720

Re: Count across variables (substring)

Mark the actual answer to the question as correct, not your thankyou post.  Now it seems that "Thank you" is the correct answer!

Super User
Super User
Posts: 7,720

Re: Count across variables (substring)

Post test data in the form of a datastep.

We are not here to type it in for your or guess what it should look like.

If you only have one 4 per item then simply:

data have;
  input pid date1 $ date2 $ date3 $;
  tmpstr=lengthn(compress(cats(of date:),'4','k'));
datalines;
1       142      121     141
2       111       12B    121
3       141       14A    121
;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 133 views
  • 2 likes
  • 3 in conversation