Help using Base SAS procedures

proc SQL, simple division(a/b)

Reply
Occasional Contributor
Posts: 9

proc SQL, simple division(a/b)

Hi there,

I would like to know if it is possible to write two proc SQL code blocks and store the two results into two different variables.Then create a third variable to store the division of the previous two variables.

Basically, I would like to count in column "status" all the "Yes" and store in variabable "a" and do another count in column "statu" but this time count all records and store the result in variable in "b".

Create a third variable  "c" to store the  a/b result.

Can this be done in one proc sql code block?

Any help or guidance is much appreciated.

Thank you

PROC Star
Posts: 7,364

proc SQL, simple division(a/b)

Is the following what you are trying to do?

proc sql;

  create table want as

    select sum(sex='M') as a,

           count(*) as b,

           calculated a/calculated b as c

      from sashelp.class

  ;

quit;

Occasional Contributor
Posts: 9

proc SQL, simple division(a/b)

Yes, except I would also like to use a group by clause somewhere in there. (sorry if I didn't mention it initially)

So, on the actual output table or view, I would like to group the a/b based on each program area, and there are about 20 program areas and I don't want to do this 20 times.

Will it be....

proc sql;

create table as resultSet as

select ProgramAreas, sum(sex='M') as a,

count(*) as b,

calculated a/calculated b as c

from table

group by Program Areas;

run;

Will this run without errors?

PROC Star
Posts: 7,364

Re: proc SQL, simple division(a/b)

As long as you spell ProgramAreas consistently (you didn't).  The following worked for sashelp.class:

proc sql;

  create table want as

    select age,

           sum(sex='M') as a,

           count(*) as b,

           calculated a/calculated b as c

      from sashelp.class

        group by age

  ;

quit;

p.s.  end your proc sql calls with quit;  not run;

Occasional Contributor
Posts: 9

proc SQL, simple division(a/b)

Great, thank you. Will try it out tomorrow morning.

Occasional Contributor
Posts: 9

proc SQL, simple division(a/b)

Hi there,

can you explain what this line of code mean?

calculated a/calculated b as c

I"m having a hard time following it

PROC Star
Posts: 7,364

proc SQL, simple division(a/b)

It simply represents a/b but, since a and b are defined in the previous line they won't be accepted unless they are preceded by the string 'calculated'.

Thus 'calculated a' represents 'a that was calculated earlier'.

Ask a Question
Discussion stats
  • 6 replies
  • 5008 views
  • 0 likes
  • 2 in conversation