Help using Base SAS procedures

This SQL works in SQL Server Management Studio but not in SAS PROC SQL... ?

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

This SQL works in SQL Server Management Studio but not in SAS PROC SQL... ?


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!


Accepted Solutions
Solution
‎11-01-2013 02:20 PM
Contributor
Posts: 27

Re: This SQL works in SQL Server Management Studio but not in SAS PROC SQL...  ?

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


All Replies
Regular Contributor
Posts: 244

Re: This SQL works in SQL Server Management Studio but not in SAS PROC SQL... ?

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).

Contributor
Posts: 27

Re: This SQL works in SQL Server Management Studio but not in SAS PROC SQL... ?

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;

Regular Contributor
Posts: 244

Re: This SQL works in SQL Server Management Studio but not in SAS PROC SQL... ?

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.)

Solution
‎11-01-2013 02:20 PM
Contributor
Posts: 27

Re: This SQL works in SQL Server Management Studio but not in SAS PROC SQL...  ?

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!

Regular Contributor
Posts: 244

Re: This SQL works in SQL Server Management Studio but not in SAS PROC SQL...  ?

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.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 887 views
  • 3 likes
  • 2 in conversation