BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
epibio2021
Obsidian | Level 7

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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; 

 

View solution in original post

9 REPLIES 9
ChrisNZ
Tourmaline | Level 20

It seems to me that you need one result variable for each character variable. You can use another array for results. 

epibio2021
Obsidian | Level 7
How do I go about creating another results array? Sorry, I am very new at SAS.
Reeza
Super User
What's the question you're trying to answer here? I have a strong suspicion you're doing extra work that isn't needed at all.
If you're looking to create a metadata document for you data for example, using SASHELP.VCOLUMNS (or dictionary.columns) is an easier method. If you're trying to find the largest length of variables that's also a different problem.
epibio2021
Obsidian | Level 7
Hi,

I'm trying to determine the level of detail of each observation, where the level of detail equates the number of characters in a character string. So I need to generate one result variable for each (22) character variable but I don't know how.
ChrisNZ
Tourmaline | Level 20

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; 

 

Reeza
Super User
A variable would represent a column. Do you need a single value for each column or do you need a value for each row?
I think your terminology maybe doesn't align with what I'm used to here.
epibio2021
Obsidian | Level 7

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. 

Reeza
Super User

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;

 

 

ballardw
Super User

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.

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
  • 9 replies
  • 4045 views
  • 2 likes
  • 4 in conversation