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

I am trying to regroup the age variable into a new variable called Age_Group. I keep getting errors when I try to run. Here is how I added a new variable:

 

data work.new;

set work.working_set;

Age_Group=Age;

run;

 

And here is the code that I am trying to run.

 

data work.new1;

set new_data;

if Age_Group LE 29 then Age_Group='18-29';

if Age_Group GE 30 AND LE 39 then Age_Group='30-39';

if Age_Group GE 40 AND LE 49 then Age_Group='40-49';

if Age_Group GE 50 AND LE 59 then Age_Group='50-59';

if Age_Group GE 60 AND  LE 69 then Age_Group='60-69';

if Age_Group GE 70 AND LE 79 then Age_Group='70-79';

if Age_Group GE 80 then Age_Group='80+';

run;

 

Can anyone help or give suggestions? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
proc format;
    value agef low-29='18-29' 30-39='30-39' /* you type the rest */ ;
run;
data new1;
    set new;
    format age agef.;
run;
--
Paige Miller

View solution in original post

4 REPLIES 4
Reeza
Super User
  • The first data step converts the AGE_GROUP variable to AGE, a numeric variable. 
  • The second step attempts to recode the AGE_GROUP variable to a character variable. 
  • AGE should be referenced for each comparison otherwise it compares to the output of the previous comparison
  • A variable cannot be converted with the same name.
  • The first data step is not required.

 

There is also inconsistency in the data sets referenced.

 

First data step:

input data set: working_set

output data set: new

 

Second data step:

input data set: new_data (not shown, or typo? No relation to previous step)

output data set: new1

 

data work.new;
set working_set;

if age LE 29 AND not missing(age) then Age_Group='18-29';
else if age GE 30 AND age LE 39 then Age_Group='30-39';

*simpler way to write the else if;
*else if 30<=age <=39 then Age_Group='30-39';*


else if age GE 40 AND age LE 49 then Age_Group='40-49';
else if age GE 50 AND age LE 59 then Age_Group='50-59';
else if age GE 60 AND age LE 69 then Age_Group='60-69';
else if age GE 70 AND age LE 79 then Age_Group='70-79';
else if age GE 80 then Age_Group='80+';

run;

 

 


@madisonahrens wrote:

I am trying to regroup the age variable into a new variable called Age_Group. I keep getting errors when I try to run. Here is how I added a new variable:

 

data work.new;

set work.working_set;

Age_Group=Age;

run;

 

And here is the code that I am trying to run.

 

data work.new1;

set new_data;

if Age_Group LE 29 then Age_Group='18-29';

if Age_Group GE 30 AND LE 39 then Age_Group='30-39';

if Age_Group GE 40 AND LE 49 then Age_Group='40-49';

if Age_Group GE 50 AND LE 59 then Age_Group='50-59';

if Age_Group GE 60 AND  LE 69 then Age_Group='60-69';

if Age_Group GE 70 AND LE 79 then Age_Group='70-79';

if Age_Group GE 80 then Age_Group='80+';

run;

 

Can anyone help or give suggestions? Thank you!


 

PaigeMiller
Diamond | Level 26
proc format;
    value agef low-29='18-29' 30-39='30-39' /* you type the rest */ ;
run;
data new1;
    set new;
    format age agef.;
run;
--
Paige Miller
madisonahrens
Calcite | Level 5

That worked! Thank you so much @PaigeMiller 

SteveDenham
Jade | Level 19

Why are you categorizing a perfectly good continuous variable?  From a statistical point of view, it is the equivalent of taking a Lamborghini into the shop and asking them to cut it up into pieces that might fit into the garage better.  See this link:https://discourse.datamethods.org/t/categorizing-continuous-variables/3402 

 

SteveDenham

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!
What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 581 views
  • 4 likes
  • 4 in conversation