Hello,
I am trying to count the number of characters in 22 variables. As an example, variable 'comments' has values like 'very good', 'excellent', and 'poor service'. I want to generate a variable that will count all the characters in the 'comments' variable and will return something like this:
very good - 9 characters
excellent - 9 characters
poor service - 12 characters.
I have to do this for 21 other variables, so I created a do loop however my output is wrong. Whatever variable I specify in proc print, returns the same character count for every variable. I suspect I have to create 22 output variables, but I am unsure how.
data want;
set have;
array count $500 _CHARACTER_
comments var1 var2 var3 ...var22;
do over count;
result = klength(count);
put result = ;
end;
run;
proc print data=want;
var comments var1 var2 ...var22 result;
run;
When this code is done for a single variable, it returns the correct output but I do not want to do the code 22 times, so I am looking for a more efficient way.
data want;
set have;
result = klength(comments);
run;
proc print data=have;
var comments result;
run;
How do I change my do loop code to reflect the correct character count output for each variable?
Thank you in advance!
This should get you started:
data WANT;
set SASHELP.CLASS;
array CHR[*] _character_ ;
array LEN[2] ;
do I=1 to dim(CHR) ;
LEN[I] = klength(CHR[I]);
end;
run;
It seems to me that you need one result variable for each character variable. You can use another array for results.
This should get you started:
data WANT;
set SASHELP.CLASS;
array CHR[*] _character_ ;
array LEN[2] ;
do I=1 to dim(CHR) ;
LEN[I] = klength(CHR[I]);
end;
run;
I need to generate the following results column:
var1 result
Obs yes 3
No 2
var2 result2
Obs good 4
bad 3
amazing 7
I hope this helps.
It's better, now my assumption is you want for each variable the unique values and the length of each of the unique variables?
Is this a good representative of what you'd want? Run the code and let me know if the data set LONG is what you want. It has more information than I think you need but also includes exactly what you want.
I'm using the Framingham Heart dataset and any character variables. To use this for your data set, replace the name of the data set in the PROC FREQ and everything else should work as is.
ods select none;
ods output onewayfreqs = uniqueValues;
proc freq data=sashelp.heart;
table _character_;
run;
ods select all;
data long;
length variable $32. variable_value $50.;
set uniqueValues;
Variable=scan(table, 2);
Variable_Value=strip(trim(vvaluex(variable)));
Variable_Quality = length(variable_value);
presentation=catt(frequency, " (", trim(put(percent/100, percent7.1)), ")");
keep variable variable_value frequency percent cum: presentation variable_quality;
label variable='Variable' variable_value='Variable Value' variable_quality = 'Variable Length/Quality';
run;
I might try
data want; set have; array count {*} comments var1- var22; array result {23}; do i=1 to dim( count); result[i] = klength(count[i]); end;
drop i; run; proc print data=want; var comments var1 var2 ...var22 result: ; run;
The Do Over really doesn't allow working with two arrays in a reliable manner.
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.