Hi:
I want to create a new field with a value determined by a formula applied to the character length of another field.
The field, called targets, contains a series of three characters, separated by a space, except for the end (e.g., KOR TUR THA ITA VNM USA CHN IND FRA POL MEX MYS IDN). In Excel the formula is easy: =(LEN(E2)+1)/4; where E2 is the cell referenced. In the Excel formula, you simply add 1 to make up for the lack of a trailing blank and then divid by four to get the number of 3-character targets. For that example, the value is 13.
There are hundreds of thousands of rows in the dataset with varying 3-character counts in each.
Is there a way to do that in SAS? Thanks
Are you eventually looking for the countw() function documented here?
data sample;
string='KOR TUR THA ITA VNM USA CHN IND FRA POL MEX MYS IDN';
n_terms=countw(string);
run;
proc print;
run;
Are you eventually looking for the countw() function documented here?
data sample;
string='KOR TUR THA ITA VNM USA CHN IND FRA POL MEX MYS IDN';
n_terms=countw(string);
run;
proc print;
run;
You should be able to do the same:
value=(length(targets) + 1)/4;
Your formula won't work for an empty string.
data sample;
string='KOR TUR THA ITA VNM USA CHN IND FRA POL MEX MYS IDN';
n_terms=countw(string);
n_terms2=(length(string) + 1)/4;
n_terms3=lengthn(compress(string))/3;
output;
string=' ';
n_terms=countw(string);
n_terms2=(length(string) + 1)/4;
n_terms3=lengthn(compress(string))/3;
output;
stop;
run;
proc print;
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.