Calcite | Level 5

## Finding average of x and y for each group

Hello, upon a question in a practice exam, I am stumped on this:

Open the existing program, program48.sas from folder C:\cert\errors. At any time, you may save your corrected program as program48 in C:\cert\programs.

This program is intended to:

• Create 3 groups for Cvar: A-G is Group=1; H-N is Group=2; O-Z is Group=3 .
• All variations of the variable should be in the same group, i.e. “A” and “a” should be in Group=1.
• Calculate the average of X and Y by Group.

Here is my code below:

```data groups;
set cert.input48;
if upcase(cvar) in ('A','B','C','D','E','F','G') then group=1;
else if upcase(cvar) in ('H','I','J','K','L','M','N') then group=2;
else group=3;
format y 10.;
run;

/* Calculate the average of X and Y by Group */
/* What is the average of X and Y for Group 2? */

proc means data=groups MEAN maxdec=2;
class group;
var x y;
run;```

Upon running this, I get:

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc means data=groups MEAN maxdec=2;
74 class group;
75 var x y;
ERROR: Variable y in list does not match type prescribed for this list.
76 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE MEANS used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

77
78
79 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
91

I've tried converting y to numeric but I do not know if this was the right move. Any help is appreciated. Thank you!

14 REPLIES 14
Barite | Level 11

## Re: Finding average of x and y for each group

I have not seen the data, but assuming x and y are numeric.  do a proc contents to check.  `Untested code!`

`````` proc format;
value \$cvar
'A','B','C','D','E','F','G'=1
'H','I','J','K','L','M','N'=2;
other=3;
run;
proc summary nway data=cert.input48;
class cvar;
var x y;
output=avgs
mean=;
format cvar \$cvar. x y 10.;
run;``````

Onyx | Level 15

## Re: Finding average of x and y for each group

Hi,

the "ERROR: Variable y in list does not match type prescribed for this list." sounds like `y` isn't numeric. Try for example something like:

``yNew = input(y, best32.);``

to convert character to numeric and try to rerun proc means with `var x yNew.

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation

Pyrite | Level 9

## Re: Finding average of x and y for each group

this is a very nice and keen observation many thanks
Meteorite | Level 14

## Re: Finding average of x and y for each group

Hi @J_Moose

You need to convert y, which is an character variable, to a numeric one.

To do that, you just need to compute another variable (y2) which the "numeric" equivalent:

``y2 = input(y,best.);``

And then adapt the PROC MEANS to use the new numeric variable y2

``````proc means data=groups mean maxdec=2;
class group;
var x y2;
run;``````
Super User

## Re: Finding average of x and y for each group

This bit of code

```if upcase(cvar) in ('A','B','C','D','E','F','G') then group=1;
else if upcase(cvar) in ('H','I','J','K','L','M','N') then group=2;
else group=3;```

Is not equivalent to:

• Create 3 groups for Cvar: A-G is Group=1; H-N is Group=2; O-Z is Group=3 .

Reason: missing values or any non-letter character would be assigned to group 3. The instructions say only the letters O to Z.

If I were grading an assignment with this you would get a deduction because of that.

One of the obnoxious things about dealing with some data sources is that they can change on you. The first set you get this might work with no missing and only letter values but the next time you might find other values. Be very careful that you know the contents of your data and the rules when dropping all other records into an "else" assignment.

Calcite | Level 5

## Re: Finding average of x and y for each group

Hi ballardw,

Any suggestions on how to improve this within the code? Thank you for pointing this out!

Barite | Level 11

## Re: Finding average of x and y for each group

I will modify my previously suggested code based on the point @ballardw made.  Convert the char value to their byte values

``````* Untested code;
data test;
set cert.input48;
nvar=rank(cvar);
if isnumber(x) then;
xn=input(x,best.);  else call missing(xn);
if isnumber(y) then;
yn=input(y,best.);  else call missing(yn);
run;
proc format;
value nvarf
71-97, 97-103='1'
72-78,104-110='2'
79-90,111-122='3'
other='.';
run;

proc summary nway data=test;
class nvar;
var x y;
output=avgs
mean=;
format nvar nvarf. xn yn 10.;
run;``````
Calcite | Level 5

## Re: Finding average of x and y for each group

Hi ghosh,

When I run this code, primarily in the proc format function it tells me that "Start is greater than end: 97-71".

Barite | Level 11

## Re: Finding average of x and y for each group

oops typo, just reverse them. Not having the data I had no way to test the code. They are simply the numerical values of the alphabet

Diamond | Level 26

## Re: Finding average of x and y for each group

The starting value of a format range must be less than the final value of your format range. 97 is not less than 71

--
Paige Miller
Pyrite | Level 9

## Re: Finding average of x and y for each group

Hi can try this hope it will solve the question

``````data groups(rename=(y2=y));
set cert.input48;
if upcase(cvar) in ('A','B','C','D','E','F','G') then group=1;
else if upcase(cvar) in ('H','I','J','K','L','M','N') then group=2;
else group=3;
y2 = input(y,best.);
drop y;
run;

proc means data=groups mean maxdec=2 noprint;
class group;
var x y;
output out=progy48(drop=_type_ _freq_)  mean=m_x mean=m_y;
run;
``````
Calcite | Level 5

## Re: Finding average of x and y for each group

Hello everyone,

I need really help. I am unable to find where to find the program of the question below.

Open the existing program, program48.sas from folder cert\errors. At any time, you may save your corrected program as program48 in cert\programs.

This program is intended to:

- Create 3 groups for Cvar: A-G is Group=1; H-N is Group=2; O-Z is Group=3 .
- All variations of the variable should be in the same group, i.e. “A” and “a” should be in Group=1.
- Calculate the average of X and Y by Group.

There are multiple errors in the program. These may be syntax errors, logic errors, or problems with the program structure. Logic errors might not produce messages in the log, but will cause the program to produce results different than intended.

What is the average (mean) of X for Group=2?
Enter your answer to the nearest whole number.

This above question was listed in https://www.analyticsexam.com/ while I was doing practice in the goal to prepare my SAS base programming certification. I could not find the program48.sas neither the cert\errors folder. I am using SAS studio. I am really new in SAS. Your help will be really appreciated.

Thank you in advance!

Super User

## Re: Finding average of x and y for each group

Hello everyone,

I need really help. I am unable to find where to find the program of the question below.

Open the existing program, program48.sas from folder cert\errors. At any time, you may save your corrected program as program48 in cert\programs.

This program is intended to:

- Create 3 groups for Cvar: A-G is Group=1; H-N is Group=2; O-Z is Group=3 .
- All variations of the variable should be in the same group, i.e. “A” and “a” should be in Group=1.
- Calculate the average of X and Y by Group.

There are multiple errors in the program. These may be syntax errors, logic errors, or problems with the program structure. Logic errors might not produce messages in the log, but will cause the program to produce results different than intended.

What is the average (mean) of X for Group=2?
Enter your answer to the nearest whole number.

This above question was listed in https://www.analyticsexam.com/ while I was doing practice in the goal to prepare my SAS base programming certification. I could not find the program48.sas neither the cert\errors folder. I am using SAS studio. I am really new in SAS. Your help will be really appreciated.

Thank you in advance!

You should start your own thread. You have a very different issue than the topic of this thread. This thread is asking details of solution code, your question is access to files from third-party (non-SAS vendor), which should be brought up to the support from that vendor.

Calcite | Level 5

## Re: Finding average of x and y for each group

The question is clearly said, "Calculate the average of X and Y by Group." BY group.

You should proc means the data by group (where group=2).

proc means data=groups mean maxdec=2;
where group=2;
class group;
var x yNew;
run;

Discussion stats
• 14 replies
• 4355 views
• 3 likes
• 9 in conversation