BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
raja777pharma
Fluorite | Level 6

Hi Team,

 

I would like to count the records based on group vars and another var VALUE is not missing.

 

My data :

data my_data;
	input subject $ test $ visit $ seq value;
	datalines;
001 PULSE VISIT1 01 70
001 PULSE VISIT1 02 80
001 DBP VISIT1 01 70
001 DBP VISIT1 . .
001 WEIGHT VISIT1 01 70
001 WEIGHT VISIT1 02 80
002 PULSE VISIT1 . .
002 PULSE VISIT1 02 80
002 DBP VISIT1 01 70
002 DBP VISIT1 02 80
002 WEIGHT VISIT1 . .
002 WEIGHT VISIT1 02 80
	;
	run;
proc sql noprint;
  create table output as
  select *, count(subject) as count
  from my_data
  where value ^= .
  group by subject, test, visit  
  ;
quit;

I am getting below output from above proc sql code:

raja777pharma_0-1649562050192.png

 

But i want below out put with out losing observations to count VISIT values for each subejct by TEST where  VALUE will be not missing.

 

Expected output:

raja777pharma_1-1649562235083.png

 

How to do in proc sql to get above output.

 

Thank you,

Rajasekhar.

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Are you trying to count observations or distinct subjects? 

If you want to count distinct subjects use the DISTINCT keyword.

count(distinct subject)

If you want to ignore the subjects with missing values use a CASE clause, not a WHERE clause

count(distinct case when (not missing(value)) then subject end)

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Are you trying to count observations or distinct subjects? 

If you want to count distinct subjects use the DISTINCT keyword.

count(distinct subject)

If you want to ignore the subjects with missing values use a CASE clause, not a WHERE clause

count(distinct case when (not missing(value)) then subject end)
Ksharp
Super User

proc sql ;
  create table output as
  select *, count(value) as count
  from my_data
  group by subject, test, visit  
  ;
quit;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 2 replies
  • 1702 views
  • 0 likes
  • 3 in conversation