## Numeric Transformations

Solved
Occasional Contributor
Posts: 9

# Numeric Transformations

Hi—

I’m very new to using Base SAS and Pro SQL to create Numeric transformations. I’m having a little trouble figuring this out.

I am using a survey were each response translates into a point value. The data set has I have 500 observations each belong to an agency. After the dataset is recoded, the data set looks like this.

 Agency Q1n Q1d Q2n Q2d Q3n Q3d Q4n Q4d Q5n Q5d AgencyA 1 1 1 1 0 2 1 2 0 0 AgencyA 0 0 2 2 1 1 2 1 2 2 AgencyA 0 0 0 0 0 0 0 0 0 0 AgencyB 1 1 1 1 1 1 0 1 1 1 AgencyB 0 0 0 0 0 0 1 0 0 0 AgencyC 2 2 1 2 0 0 2 0 2 2 AgencyC 1 1 2 2 0 2 2 2 1 1 AgencyC 2 2 1 1 2 2 2 2 1 2 AgencyD 1 1 1 1 1 1 0 1 1 1 AgencyD 1 1 1 2 1 1 2 1 1 2 AgencyD 1 1 2 2 1 1 1 1 1 2 AgencyD 0 0 0 0 0 0 0 0 0 0

Then I have to generate a score for each agency. So, then I need to add up all the QNs and then add up all the QDs and then divide which awards the score. This has to be aggregated by agency.

Total Agency Sum Numerator=Q1N + Q2N + Q3N + Q4N + Q5N + Q6N + Q7N + Q8N + Q9N + Q10N+ Q11N

Total Agency Sum Denominator =Q1D + Q2D + Q3D + Q4D + Q5D + Q6D + Q7D + Q8D + Q9D + Q10N + Q11N

Agency Score= Total Agency Sum Numerator Numerator / Total Agency Sum Numerator Denominator.

Below is the code I have – I’m having a little trouble, figuring this out.

BASE SAS

data  have;

set want;

Sumn= sum(Q1N, Q2N, Q3N ,Q4N, Q5N ,Q6N ,Q7N ,Q8N ,Q9N, Q11N);

Sumd= sum(Q1D, Q2D, Q3D ,Q4D, Q5D ,Q6D ,Q7D ,Q8D ,Q9D, Q11D);

group by***what is the equivalent of group by for base SAS?*** AGENCY;

run;

PROC SQL

proc sql;

create table want as

select distinct

AGENCY,

sum(Q1N, Q2N, Q3N ,Q4N, Q5N ,Q6N ,Q7N ,Q8N ,Q9N, Q11N) as Sumn,

sum(Q1D, Q2D, Q3D ,Q4D, Q5D ,Q6D ,Q7D ,Q8D ,Q9D, Q11D) as Sumd,

calculated sum(Sumn) as sumtn,

calculated sum(Sumd) as sumtd

from have

group by AGENCY;

quit;

THANKS!!!!!!!!

Accepted Solutions
Solution
‎06-04-2012 11:00 AM
Posts: 3,167

## Re: Numeric Transformations

for a SQL approach:

proc sql;

create table want as

select distinct

*,sum(sum(Q1N, Q2N, Q3N ,Q4N, Q5N)) as Sumn,

sum(sum(Q1D, Q2D, Q3D ,Q4D, Q5D)) as Sumd, calculated sumn/calculated sumd as score

from have

group by AGENCY;

quit;

Haikuo

All Replies
Posts: 3,167

## Re: Numeric Transformations

Is this what you want:

data have;

input agency\$7. Q1n Q1d Q2n Q2d Q3n Q3d Q4n Q4d Q5n Q5d;

cards;

AgencyA 1 1 1 1 0 2 1 2 0 0

AgencyA 0 0 2 2 1 1 2 1 2 2

AgencyA 0 0 0 0 0 0 0 0 0 0

AgencyB 1 1 1 1 1 1 0 1 1 1

AgencyB 0 0 0 0 0 0 1 0 0 0

AgencyC 2 2 1 2 0 0 2 0 2 2

AgencyC 1 1 2 2 0 2 2 2 1 1

AgencyC 2 2 1 1 2 2 2 2 1 2

AgencyD 1 1 1 1 1 1 0 1 1 1

AgencyD 1 1 1 2 1 1 2 1 1 2

AgencyD 1 1 2 2 1 1 1 1 1 2

AgencyD 0 0 0 0 0 0 0 0 0 0

;

data want;

do until (last.agency);

set have;

by agency notsorted;

if first.agency then call missing(sumn,sumd);

sumn=sum(sumn,q1n,q2n,q3n,q4n,q5n);

sumd=sum(sumd, q1d,q2d,q3d,q4d,q5d);

end;

if sumd ne 0 then score=sumn/sumd;

do until (last.agency);

set have;

by agency notsorted;

output;

end;

drop sum:;

run;

proc print;run;

Haikuo

Occasional Contributor
Posts: 9

## Re: Numeric Transformations

Great!!!!! Quick questions if you have time.

I'm not toally sure what these parts in the code mean:

These parts:

if first.agency then call missing(sumn,sumd);

do until (last.agency);

And this part:

notsorted; output; end;

Thanks!!!

Posts: 3,167

## Re: Numeric Transformations

if first.agency then call missing(sumn,sumd);

sumn and sumd have been retained by using sum(), so their value will be rolled over to next agency, which is not what you want, so at beginning of every agency, you make them either missing or '0'.

do until (last.agency);

please search 'DOW' in literatures, you will get an idea. It is not something I can explain in two sentences.

notsorted; output; end;

notsorted is an option used when your obs grouping together but not necessarily sorted

output: please search output statement in SAS doc. Here to override the default output happening at the end of each do-loop.

end is the end of do-loop.

Oh, boy, from your questions, do tell you are very new to SAS. but don't worry, we are here to help.

Haikuo

Solution
‎06-04-2012 11:00 AM
Posts: 3,167

## Re: Numeric Transformations

for a SQL approach:

proc sql;

create table want as

select distinct

*,sum(sum(Q1N, Q2N, Q3N ,Q4N, Q5N)) as Sumn,

sum(sum(Q1D, Q2D, Q3D ,Q4D, Q5D)) as Sumd, calculated sumn/calculated sumd as score

from have

group by AGENCY;

quit;

Haikuo

Super Contributor
Posts: 1,636

## Re: Numeric Transformations

Hi Haikuo,

I am curious why did you use "distinct" in your code?  Thanks - Linlin

proc sql;

create table want as

select distinct

*,sum(sum(Q1N, Q2N, Q3N ,Q4N, Q5N)) as Sumn,

sum(sum(Q1D, Q2D, Q3D ,Q4D, Q5D)) as Sumd, calculated sumn/calculated sumd as score

from have

group by AGENCY;

quit;

Posts: 3,167

## Re: Numeric Transformations

LinLin,

You always seem to be able to pick up something. I 'd admit, I have made my code out of OP's original version, which has 'distinct', and I didn't even notice that. . Maybe OP wants to remove duplicates in the original table at the same time calculate the score.

Haikuo

Posts: 5,519

## Re: Numeric Transformations

There are many wasy, of course, here is one :

data want(keep=AGENCY sumn sumd score);
do until (last.AGENCY);
set have;
by AGENCY;
Sumn= sum(Sumn, Q1N, Q2N, Q3N ,Q4N, Q5N,Q6N ,Q7N ,Q8N ,Q9N, Q11N);
Sumd= sum(Sumd, Q1D, Q2D, Q3D ,Q4D, Q5D,Q6D ,Q7D ,Q8D ,Q9D, Q11D);
end;
score = Sumn/Sumd;
run;

proc sql;
create table wantSQL as
select
AGENCY,
sum(sum(Q1N, Q2N, Q3N ,Q4N, Q5N ,Q6N ,Q7N ,Q8N ,Q9N, Q11N)) as Sumn,
sum(sum(Q1D, Q2D, Q3D ,Q4D, Q5D ,Q6D ,Q7D ,Q8D ,Q9D, Q11D)) as Sumd,
calculated Sumn / calculated Sumd as score
from have
group by AGENCY;
quit;

PG

PG
Occasional Contributor
Posts: 9

## Re: Numeric Transformations

Thanks! I'm have trouble understanding what first. and last. .

Posts: 5,519

## Re: Numeric Transformations

When the SET statement reads from a dataset sorted by AGENCY,

first.AGENCY equals 1 when the most recent SET statement read the first observation of an agency, otherwise it equals 0,

last.AGENCY equals 1 when the most recent SET statement read the last observation of an agency, otherwise it equals 0.

Both first. and last. varables can equal 1 at the same time when an AGENCY has only one observation.

PG

PG
🔒 This topic is solved and locked.

Discussion stats
• 9 replies
• 394 views
• 3 likes
• 4 in conversation