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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.