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

Apologies in advance, I am very new to SAS and am having trouble with an assignment. 

I'm getting error 161-185 no matching DO/SELECT statement. The assignment is described below: 

Within the datastep create the following new variables:
(a) A new variable called agecat that divides age into 5 categories:45-49;50-54;55-59;60-
64;65-69. Give values 1-5 for the categories.
(b) A new variable called income40 equal to 0 if income is less than $40,000 and equal to
1 if income is $40,000 or more.
(c) A new variable called collgrad equal to 0 if the participant did not graduate from college
and equal to 1 if the participant graduated from college (see study forms for income and
education categories)
(d) A new variable that is the lowest of the two serum potassium values

 

This is my SAS code right now (still haven't gotten to parts C and D):

 

libname t '/home/u58860515/';
data tdata; set t.tomhs (keep=age sex income educ hdlbl potassbl potass12);
if age = 45-49 then agecat = 1; else
if age = 50-54 then agecat = 2; else
if age = 55-59 then agecat = 3; else
if age = 60-64 then agecat = 4; else
if age = 65-69 then agecat = 5; end;
if income < 40000 then income40 = 0; else
if income > 40000 then income40 = 1; end;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

 

if age = 45-49 then agecat = 1;

This statement does a subtraction (that's what SAS thinks when it sees the character –), and tests to see if age is equal to the subtraction 45 minus 49 which is –4.

 

I think what you want to do is:

if age<=49 and age>=45 then agecat=1;

 

An equivalent version of the above, which is less typing is:

if 45<=age<=49 then agecat=1;

 

In this case, for most analyses, you don't really need to create a new variable anyway. Using formats allows most SAS procedure to produce the value of 1 whenever you have ages between 45 and 49:

 

proc format;
    value agef 45-49 = '1' 50-54 = '2' ... /* I didn't type the rest*/ ;
run;

and then you assign the format agef to your variable age in a data step or PROC

 

format age agef.;

But why use the relatively meaningless value of 1 to indicate an age group????? This is a poor idea. Make your decisions so the output is more readable and meaningful. This code make the text string '45-49' appear wherever the ages are in that group and also allows most SAS procedures to operate on the group of ages between 45-49 as a single category.

 

proc format;
    value agef 45-49 = '45-49' 50-54 = '50-54' ... /* I didn't type the rest*/ ;
run;

So, no new variable needed to create these age categories, and a more readable result can be obtained as well. The whole idea of creating categories named 1, 2, 3 ... really doesn't make sense here; categories named '45-49' etc make a whole lot more sense.

 

If this is a homework assignment, then I consider it to be a very poor assignment, as creating a new variable which has a value of 1 for the first age group is not a good thing to be encouraging; using formats would be a much better approach.

--
Paige Miller

View solution in original post

3 REPLIES 3
Astounding
PROC Star

You're well on your way to solving this.  Just get rid of the "end;" statement in both places.  Then continue creating the remaining variables.

 

"end;" is only needed when there is a DO or SELECT statement, as the error message indicates.

PaigeMiller
Diamond | Level 26

 

if age = 45-49 then agecat = 1;

This statement does a subtraction (that's what SAS thinks when it sees the character –), and tests to see if age is equal to the subtraction 45 minus 49 which is –4.

 

I think what you want to do is:

if age<=49 and age>=45 then agecat=1;

 

An equivalent version of the above, which is less typing is:

if 45<=age<=49 then agecat=1;

 

In this case, for most analyses, you don't really need to create a new variable anyway. Using formats allows most SAS procedure to produce the value of 1 whenever you have ages between 45 and 49:

 

proc format;
    value agef 45-49 = '1' 50-54 = '2' ... /* I didn't type the rest*/ ;
run;

and then you assign the format agef to your variable age in a data step or PROC

 

format age agef.;

But why use the relatively meaningless value of 1 to indicate an age group????? This is a poor idea. Make your decisions so the output is more readable and meaningful. This code make the text string '45-49' appear wherever the ages are in that group and also allows most SAS procedures to operate on the group of ages between 45-49 as a single category.

 

proc format;
    value agef 45-49 = '45-49' 50-54 = '50-54' ... /* I didn't type the rest*/ ;
run;

So, no new variable needed to create these age categories, and a more readable result can be obtained as well. The whole idea of creating categories named 1, 2, 3 ... really doesn't make sense here; categories named '45-49' etc make a whole lot more sense.

 

If this is a homework assignment, then I consider it to be a very poor assignment, as creating a new variable which has a value of 1 for the first age group is not a good thing to be encouraging; using formats would be a much better approach.

--
Paige Miller
Sajid01
Meteorite | Level 14

Hello
There are many approaches to solving your issue.
I would prefer by creating formats and using them. With your case in mind, I would have the following code to create formats

proc format;
	value **bleep**e low-45='LOW' 45-49=1 50-54=2 55-59=3 60-64=4 65-69=5 69-high='High';
	value fincome Low - < 40000=0 40000 - high=1;
	value $grad "N"=0 "Y"=1 Other="Missing";
run;

Points to note.
If age is less then 45 or missing, you will get a "LOW". Similarly for age greater than 69 you will see high.

For income less than 40000 includes missing values.
I have assumed that for college graduation your data has a response of 'Y' or 'N". Accordingly the values are set.
If this value is missing you will  "Missing".

You need to code based on your detailed logic and data.

For somebody  new to SAS, I would recommend this training from SAS https://support.sas.com/edu/schedules.html?crs=PROG1&ctry=US . This gives a good understanding of SAS.

Having a copy of at least one of the books  "The SAS Little SAS Book" or "SAS Programming By Example" is very helpful and saves time.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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