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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.