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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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