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


Hi, All!

I am trying to build a sql query that will tell me what percent of a column in a sql server table is populated - i.e. non-missing count / total count.

When I run this sql in Microsoft SQL Server Management Studio:

select sum(case

                      when DocumentationType <> ' '

                      then 1

                      else 0

                  end) as num_pop

          ,count(*)  as count

          ,sum(case

                      when DocumentationType <> ' '

                      then 1.00

                      else 0.00

                    end) / count(*) as pct_pop

from raw_eve297_lps

I get these results:

num_pop   count          pct_pop

135392      525079      0.257850

When I run the same sql in a PROC SQL in SAS, I get these results:

The SAS System


num_pop     count     pct_pop
135392     525079     0

I KNOW it's something silly I'm overlooking.  Fresh sets of eyes are welcome.  Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Malarkey
Obsidian | Level 7

Hi, Snoopy!

Here is what finally worked:

proc sql;

select sum(case
             when DocumentationType ^= ' '
    then 1
    else 0
      end) as num_pop
      ,count(*)  as count
      ,put((calculated num_pop / calculated count),8.7)

from raw.raw_eve297_lps
;

quit;

I've been reading through other posts to try to find the answer, saw one where the put was used and thought I'd try it.  That did the trick - and thanks for the calculated col_name tip!  Much less clutter in the SQL statement!

View solution in original post

5 REPLIES 5
snoopy369
Barite | Level 11

What DBMS is raw_eve297_lps in?  Is that a SAS dataset?  If not it might be translating the code in some odd form.

This works:

data testdata;

set sashelp.class;

if sex='M' then sex=' ';

run;

proc sql;

select sum(case

                      when sex <> ' '

                      then 1

                      else 0

                  end) as num_pop

          ,count(*)  as count

          ,sum(case

                      when sex <> ' '

                      then 1.00

                      else 0.00

                    end) / count(*) as pct_pop

from testdata

;

quit;

I would also change <> to NE or ^=; while <> does work in PROC SQL, in other places in SAS it's the MAX operator and may confuse other SAS programmers.

Also working in SAS:

proc sql;

select sum(case

                      when sex <> ' '

                      then 1

                      else 0

                  end) as num_pop

          ,count(*)  as count

          ,calculated num_pop/calculated count

from testdata

;

quit;

You might try that and see if it works better.  (Calculated is how you refer to new columns earlier defined in that query).

Malarkey
Obsidian | Level 7

Hi, Snoopy!

It's in a sql server database.  I should have pasted my SAS code also - I am using ^= there:

libname raw      odbc DATABASE=PROD_Rawdata       

                      schema=dbo;

proc sql;

select sum(case

             when DocumentationType ^= ' '

             then 1

             else 0

            end) as num_pop

      ,count(*)  as count

      ,sum(case

             when DocumentationType ^= ' '

             then 1.00

             else 0.00

            end) / count(*) as pct_pop

from raw.raw_eve297_lps

;

quit;

snoopy369
Barite | Level 11

Well, then I don't see anything obviously wrong.  You could try turning off implicit passthrough (NOIPASSTHRU) in the PROC SQL statement and see if that helps, or see if FEEDBACK helps any in explaining what it's doing differently.  Sometimes queries involving nulls/missings/etc. are handled differently by DBMSs other than SAS.  I'd expect an issue with both elements, but perhaps something's happening that I don't fully understand in the translation to SQL Server code.  (NOIPASSTHRU would likely slow your query down, FYI, as it would have to bring all the data back to SAS before summarizing it.)

Malarkey
Obsidian | Level 7

Hi, Snoopy!

Here is what finally worked:

proc sql;

select sum(case
             when DocumentationType ^= ' '
    then 1
    else 0
      end) as num_pop
      ,count(*)  as count
      ,put((calculated num_pop / calculated count),8.7)

from raw.raw_eve297_lps
;

quit;

I've been reading through other posts to try to find the answer, saw one where the put was used and thought I'd try it.  That did the trick - and thanks for the calculated col_name tip!  Much less clutter in the SQL statement!

snoopy369
Barite | Level 11

Good to know it works.  You might just have a format issue - if PUT works, then you could even try

, calculated num_pop/calculated count format=8.7

if that's the issue.  Depends in part on where this is ultimately being consumed whether that's likely.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 2256 views
  • 3 likes
  • 2 in conversation