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

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? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

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
terjeph
Obsidian | Level 7
Thanks!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

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
  • 4 replies
  • 1732 views
  • 2 likes
  • 3 in conversation