Help using Base SAS procedures

Grouping lowest to highest when blank value exists

Accepted Solution Solved
Reply
Regular Contributor
Posts: 179
Accepted Solution

Grouping lowest to highest when blank value exists

[ Edited ]

proc sql;

create table setup_summ as

select cost_ctr,count(cnt) as cnt1

from setup

group by cost_ctr

order by cost_ctr

;quit;

 

Cost_ctr   Cnt

                   10

4320102    133

4320105    294

4320545    957

4327979    619

4327983    489

4327984    493

4327985    529

4327996    51

 

I tried defining cost_ctr as both numeric and text. As you can see if no cost_ctr exists the blank cost center goes to the top

I want the blank cost center to show at the bottom however I still want to sort by cost center from lowest to the highest. The sequence should be the same except the blank one should show at the bottom.  How can I accomplish this


Accepted Solutions
Solution
‎09-18-2017 10:24 AM
PROC Star
Posts: 514

Re: Grouping lowest to highest when blank value exists

In sql you can do something like this. 

proc sql;
select a.cost_ctr, a.cnt from 
(select cost_ctr, cnt from have)a
cross join
(select  max(cost_ctr) +1 as nmax from have)b
order by coalesce(a.cost_ctr, nmax) ;
quit;

View solution in original post


All Replies
PROC Star
Posts: 1,836

Re: Grouping lowest to highest when blank value exists

[ Edited ]

Try this:

 

proc sql;

   title 'A UNION B';

 create table setup_summ as

select cost_ctr,count(cnt) as cnt1

from setup

where your_variable ne ' ' /*Please notice here and change accordingly*/

group by cost_ctr

order by cost_ctr

   union

  select cost_ctr,count(cnt) as cnt1

from setup

where your_variable=' ' /*Please notice here and change accordingly*/

group by cost_ctr;quit;

 

Solution
‎09-18-2017 10:24 AM
PROC Star
Posts: 514

Re: Grouping lowest to highest when blank value exists

In sql you can do something like this. 

proc sql;
select a.cost_ctr, a.cnt from 
(select cost_ctr, cnt from have)a
cross join
(select  max(cost_ctr) +1 as nmax from have)b
order by coalesce(a.cost_ctr, nmax) ;
quit;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 230 views
  • 1 like
  • 3 in conversation