I trying to right a query to calculate count. Lets say I have 4 columns M1, M2, M3 and Count.with 100 rows. I want a query to calculate number values available in each row and give me count of values in Count column, excluding missing values.
Example: If row1 has values in M1, M2 and M3, Count will be 3. If in row 2 value of M2 is missing, count will be 2. Query should not be limited to number of rows, since data can sometimes have 100 rows or 1000.
Hi,
data want;
set have;
count=4 - nmiss(of m1-m4);
run;
Are your columns numeric, character or some of each?
numeric
Use the N function.
SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition
count=n(m1, m2, m3...etc);
OR
count=n(of m1-m4);
Thanks...Reeza.
What if it is based on three month value check:
A variable which tracked over three months, we neglect the missing but observe how many times the value of that variable changes, meaning if M1value was 1 and it changed to 2 in m2 and then in M3 which is the observed month it changed to 3 so total number of distinct changes are 3. So I would like query to show how many distinct changes happened to this variable. If the value stayed 2 through out then it is not a change.
This is a new question and should probably be posted as such. Also, include a sample of what you want the output to look like.
proc sql;
select *,3-cmiss(m1,m2,m3) as count from have;
quit;
What if it is based on three month value check:
A variable which tracked over three months, we neglect the missing but observe how many times the value of that variable changes, meaning if M1value was 1 and it changed to 2 in m2 and then in M3 which is the observed month it changed to 3 so total number of distinct changes are 3. So I would like query to show how many distinct changes happened to this variable. If the value stayed 2 through out then it is not a change.
Please assist:
What if it is based on three month value check:
A variable which tracked over three months, we neglect the missing but observe how many times the value of that variable changes, meaning if M1value was 1 and it changed to 2 in m2 and then in M3 which is the observed month it changed to 3 so total number of distinct changes are 3. So I would like query to show how many distinct changes happened to this variable. If the value stayed 2 through out then it is not a change.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.