BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10
data have;
item1 = 'central';
item2 = '';
item3 = 'Pacific';
run;

I want to apply a formula that gives me the total number observations by row and displays a field called row_cnt.  So in this case the total would be 2 since there are two observations.  The variables however are character in nature

3 REPLIES 3
tarheel13
Rhodochrosite | Level 12

Let me know if this is what you're looking for.

data have;
item1 = 'central';
item2 = '';
item3 = 'Pacific';
run;

proc sql;
	select count(name) into: total
		from dictionary.columns
		 where upcase(libname)='WORK' and upcase(memname)='HAVE';
quit;

%put &=total;

data have2;
	set have;
	totalvar=&total;
	num=totalvar-cmiss(of item:);
run;
ballardw
Super User

Please show what you want the output to look like.

 

Note: "Observation" is SAS for "row" or "record". So your example data only has 1 observation and 2 non-missing values.

 

One way for what I think you are asking for:

data have;
   item1 = 'central';
   item2 = '';
   item3 = 'Pacific';
   row_cnt = countw(catx('*',item1,item2,item3),'*');
   row_cnt2= countw(catx('*',of item:),'*');
run;

which creates a string of your values with * between the ones that are not missing and then uses the Countw function to count how many separations are there.

If you have more than 3 or 4 variables you might use an Array or a list such as the "of item:" to use all the variables whose names start with Item to build the string before counting.

 

Kurt_Bremser
Super User

@Q1983 wrote:

 

So in this case the total would be 2 since there are two observations.

No. You have one observation containing three variables, of which two have values.

 

(deleted non-working code)

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

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
  • 3 replies
  • 1093 views
  • 0 likes
  • 4 in conversation