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

Hello all,

 

Please can you assist with the following problem? I need to count the number of repeated characters in a dataset for each repeated "char" data.

 

Original data:

NumChar
100060002A
100060004A
100060008A
100060010B
100060013B
100060014B
100060015C
100060020C
100060021D

 

Required output:

NumCharCount
100060002A1
100060004A2
100060008A3
100060010B1
100060013B2
100060014B3
100060015C1
100060020C2
100060021D1

 

Any assitance would be greatly appreciated.

 

My code thus far:

 

DATA TEST;
	SET TEST;
	BY CHAR;
	PUT COUNT 1.;
	
	IF FIRST.CHAR THEN;
	COUNT=0;
	COUNT+1;
RUN;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

First of all, this is the way to post example data:

data have;
input num char $;
cards;
100060002 A
100060004 A
100060008 A
100060010 B
100060013 B
100060014 B
100060015 C
100060020 C
100060021 D
;
run;

It allows us to recreate your data with a simple copy/paste and run.

Next, your use of the if - then is erroneous. The semicolon immediately after the then causes the conditional statement to end.

This (applied to test data from above) will do it:

data want;
set have;
by char;
if first.char
then count = 1;
else count + 1;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

First of all, this is the way to post example data:

data have;
input num char $;
cards;
100060002 A
100060004 A
100060008 A
100060010 B
100060013 B
100060014 B
100060015 C
100060020 C
100060021 D
;
run;

It allows us to recreate your data with a simple copy/paste and run.

Next, your use of the if - then is erroneous. The semicolon immediately after the then causes the conditional statement to end.

This (applied to test data from above) will do it:

data want;
set have;
by char;
if first.char
then count = 1;
else count + 1;
run;
DanielSantos
Barite | Level 11

 

Hi, I think you've got it right, just loose the ; after the then

 


DATA TEST;
	SET TEST;
	BY CHAR;
	PUT COUNT 1.;
	
	IF FIRST.CHAR THEN COUNT=0;
	COUNT+1;
RUN;

 

Daniel Santos @ www.cgd.pt

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