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
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;
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.
@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)
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.