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

Dear all,

 

In my dataset, I have a varialbe with the observation values between 0 and 1, I want to count how many observations of this variable is blow 0.2, how many between 0.2 and 0.5, how many over 0.5. Can I have this done in one sql procedure? If yes, How?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Select

sum(var<0.2) as lt_2,

sum(var>=0.2 and var <=0.5) as bet_2_5,

etc..

from have;

 

Yes you can. Sum the condition, if the condition is true it resolves to 1, 0 otherwise. 

 

View solution in original post

4 REPLIES 4
amanda4286
Calcite | Level 5

You could use

1.)the retain function in the data step and count the values using a series of if conditions or

2.)In proc sql ,probably use the Union option  between three individual queries to obtain  the counts with the help of group by statments

Reeza
Super User

Select

sum(var<0.2) as lt_2,

sum(var>=0.2 and var <=0.5) as bet_2_5,

etc..

from have;

 

Yes you can. Sum the condition, if the condition is true it resolves to 1, 0 otherwise. 

 

Kurt_Bremser
Super User

 

data want (keep=count_a count_b count_c);
set have end=done;
retain count_a count_a count_c 0;
if var < 0.2 then count_a + 1;
else if var <= 0.5 then count_b + 1;
else count_c + 1;
if done then output;
run;

Another method is to create a format, so the formatted values can be used in a simple freq:

proc format library=work;
value testfmt
  low-<0.2 = 1
  0.2-0.5 = 2
  0.5<-high = 3
;
run;

data have;
input var;
format var testfmt.;
cards;
.1
.3
.6
.4
.5
.2
.7
;
run;

proc freq data=have order=internal;
tables var / nopercent nocum;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 13962 views
  • 6 likes
  • 4 in conversation