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

I have a data set where i grouped the age groups and then i wanted to assign a number to each group but when i run the code some of them work but then some groups don't show any numbers. Only 0-9 and 65+ works the rest of them are blank

 

DATA work.import;
SET work.import;
IF (AGE >=0) AND (AGE<=9) THEN NAGE= "0-9 YEARS";
IF (AGE >=10) AND (AGE<=17) THEN NAGE= "10-17 YEARS";
IF (AGE >=18) AND (AGE<=35) THEN NAGE= "18-35 YEARS";
IF (AGE >=36) AND (AGE<=50) THEN NAGE= "36-50 YEARS";
IF (AGE >=51) AND (AGE<=64) THEN NAGE= "51-64 YEARS";
IF (AGE >=65) THEN NAGE= "65+";
RUN;

data work.import;
set work.import;
if NAGE = "0-9 YEARS" THEN groupage=1;
if NAGE = "10-17 YEARS" THEN groupage=2;
if NAGE = "18-35 YEARS" THEN groupage=3;
if NAGE = "36-50 YEARS" THEN groupage=4;
if NAGE = "51-64 YEARS" THEN groupage=6;
if NAGE = "65+" THEN groupage=7;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Try this one:

DATA work.import;
  SET work.import;

  select;
    when ( (AGE >=0 ) AND (AGE<=9 ) ) do; NAGE= "0-9 YEARS";   groupage=1; end;
    when ( (AGE >=10) AND (AGE<=17) ) do; NAGE= "10-17 YEARS"; groupage=2; end;
    when ( (AGE >=18) AND (AGE<=35) ) do; NAGE= "18-35 YEARS"; groupage=3; end;
    when ( (AGE >=36) AND (AGE<=50) ) do; NAGE= "36-50 YEARS"; groupage=4; end;
    when ( (AGE >=51) AND (AGE<=64) ) do; NAGE= "51-64 YEARS"; groupage=5; end;
    when ( (AGE >=65)               ) do; NAGE= "65+";         groupage=6; end;
    otherwise put "ERROR: value!";
  end;
RUN;

 

One more side note: if I were you I would use not: 

(AGE >=0 ) AND (AGE<=9 )

but rather:

(AGE >=0 ) AND (AGE < 10)

 

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



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15

Try this one:

DATA work.import;
  SET work.import;

  select;
    when ( (AGE >=0 ) AND (AGE<=9 ) ) do; NAGE= "0-9 YEARS";   groupage=1; end;
    when ( (AGE >=10) AND (AGE<=17) ) do; NAGE= "10-17 YEARS"; groupage=2; end;
    when ( (AGE >=18) AND (AGE<=35) ) do; NAGE= "18-35 YEARS"; groupage=3; end;
    when ( (AGE >=36) AND (AGE<=50) ) do; NAGE= "36-50 YEARS"; groupage=4; end;
    when ( (AGE >=51) AND (AGE<=64) ) do; NAGE= "51-64 YEARS"; groupage=5; end;
    when ( (AGE >=65)               ) do; NAGE= "65+";         groupage=6; end;
    otherwise put "ERROR: value!";
  end;
RUN;

 

One more side note: if I were you I would use not: 

(AGE >=0 ) AND (AGE<=9 )

but rather:

(AGE >=0 ) AND (AGE < 10)

 

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



ballardw
Super User

The reason you only get correct values for last group is because when you use multiple IF statements with the same variable then the result from the LAST one is kept. With IF statements you would use ELSE so that when the previous test was false it go to the next one.

 

DATA work.import;
SET work.import;
     IF (AGE >=0) AND (AGE<=9) THEN NAGE= "0-9 YEARS";
Else IF (AGE >=10) AND (AGE<=17) THEN NAGE= "10-17 YEARS";
Else IF (AGE >=18) AND (AGE<=35) THEN NAGE= "18-35 YEARS";
Else IF (AGE >=36) AND (AGE<=50) THEN NAGE= "36-50 YEARS";
Else IF (AGE >=51) AND (AGE<=64) THEN NAGE= "51-64 YEARS";
Else IF (AGE >=65) THEN NAGE= "65+";
RUN;

And again, using

Data work.import;
   set work.import.

Is just asking for problems as this replaces the source data set every time you use the same data set.

All you need is one typo like

IF Age > 65;

to lose a lot of data.

 

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 589 views
  • 0 likes
  • 3 in conversation