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

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.

AgencyQ1nQ1dQ2nQ2dQ3nQ3dQ4nQ4dQ5nQ5d
AgencyA1111021200
AgencyA0022112122
AgencyA0000000000
AgencyB1111110111
AgencyB0000001000
AgencyC2212002022
AgencyC1122022211
AgencyC2211222212
AgencyD1111110111
AgencyD1112112112
AgencyD1122111112
AgencyD0000000000

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!!!!!!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

9 REPLIES 9
Haikuo
Onyx | Level 15

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

Rick79
Calcite | Level 5

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!!!


Haikuo
Onyx | Level 15

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

Haikuo
Onyx | Level 15

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

Linlin
Lapis Lazuli | Level 10

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;

Haikuo
Onyx | Level 15

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. Smiley Happy. Maybe OP wants to remove duplicates in the original table at the same time calculate the score.

Haikuo

PGStats
Opal | Level 21

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
Rick79
Calcite | Level 5

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

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 9 replies
  • 1206 views
  • 3 likes
  • 4 in conversation