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;
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 lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.