BookmarkSubscribeRSS Feed
pacman94
Calcite | Level 5

I have the following dataset. if the value is blank I need the count variable to be  to be blank. Otherwise count all the comma + 1

Have

IDVALUE
ABC2
CDE 
EEA2,3
AWE4,5,6
DDE5
QWE 
WQE5,3,2

 

Have:

iDVALUEcount
ABC21
CDE  
EEA2,32
AWE4,5,63
DDE51
QWE  
WQE5,3,23

Here's what I have

if missing(VALUE) then count = .;

else count = countc(VALUE, ',')  + 1;

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

Have you looked at function COUNTW ?

r_behata
Barite | Level 11
data have;
input ID $ VALUE $;
infile cards truncover;
cards;
ABC 2
CDE 
EEA 2,3
AWE 4,5,6
DDE 5
QWE 
WQE 5,3,2
;
run;


data want;
	set have;

	count=ifc(^ missing(VALUE),left(countw(VALUE,',')),'');
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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