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
ID | VALUE |
ABC | 2 |
CDE | |
EEA | 2,3 |
AWE | 4,5,6 |
DDE | 5 |
QWE | |
WQE | 5,3,2 |
Have:
iD | VALUE | count |
ABC | 2 | 1 |
CDE | ||
EEA | 2,3 | 2 |
AWE | 4,5,6 | 3 |
DDE | 5 | 1 |
QWE | ||
WQE | 5,3,2 | 3 |
Here's what I have
if missing(VALUE) then count = .;
else count = countc(VALUE, ',') + 1;
Have you looked at function COUNTW ?
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.