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

Hello,

I have a data looks like this below. I would like to count number of times substring 'Id' appeared in the variable Practise. I tried below code but it does not work, my code generates only 0.

 

Raw Data:

Practise
Id: John, Id: Kerstin, Name: sunny, 
Name: Kane, Id: Rohan
Id: Ranson, Name: X_, Id: Stary

Id:Ata, Name: Unknown, Id: Aka, Name, Unknown, Id: Prx

 

Expected Result:

PractiseId_Count
Id: John, Id: Kerstin, Name: sunny, 2
Name: Kane, Id: Rohan1
Id: Ranson, Name: X_, Id: Stary2
Id:Ata, Name: Unknown, Id: Aka, Name, Unknown, Id: Prx3

 

My SAS code:

 

data samp ;
set Clean;
  count_Id = count(Practice, 'Id:');
   put count_Id;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@chapidi99 What doesn't work? Your code appears to return the expected result for your sample data.

data have;
  infile datalines truncover dlm='|';
  input Practise :$100. count_expected;
  datalines;
Id: John, Id: Kerstin, Name: sunny,|2
Name: Kane, Id: Rohan|1
Id: Ranson, Name: X_, Id: Stary|2
Id:Ata, Name: Unknown, Id: Aka, Name, Unknown, Id: Prx|3
;

data want;
  set have;
  count_Id = count(Practise, 'Id:');
  count_derived_1=count(Practise,'id:','i');
run;

 

Patrick_0-1634364284017.png

 

View solution in original post

3 REPLIES 3
chapidi99
Fluorite | Level 6
Corrected code (Practise variable), but still doesn't work

data samp ;
set Clean;
count_Id = count(Practise, 'Id:');
put count_Id;
run;
Patrick
Opal | Level 21

@chapidi99 What doesn't work? Your code appears to return the expected result for your sample data.

data have;
  infile datalines truncover dlm='|';
  input Practise :$100. count_expected;
  datalines;
Id: John, Id: Kerstin, Name: sunny,|2
Name: Kane, Id: Rohan|1
Id: Ranson, Name: X_, Id: Stary|2
Id:Ata, Name: Unknown, Id: Aka, Name, Unknown, Id: Prx|3
;

data want;
  set have;
  count_Id = count(Practise, 'Id:');
  count_derived_1=count(Practise,'id:','i');
run;

 

Patrick_0-1634364284017.png

 

chapidi99
Fluorite | Level 6
@Patrick, thanks it works when I removed put statement.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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