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

Dear all,

I have a table consisting of a list of females and males as follows:

Anita_n_0-1612302355318.png

I wish to use the below subquery to get out the first and last name in one column for

for the female with the minimum age. But am not getting it working. I will appreciate

any help. Thanks

this code works fine for the minimum age for both sex

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE age=(SELECT MIN(age) FROM mytable) ;
QUIT;

 but I want to add to this code, that the minimum age should not apply to both sex but only the females.

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE sex= "female" AND age=(SELECT MIN(age) FROM mytable) ;
QUIT;

if I add this part with sex="female" I get no results

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Does this work for you? Your previous query was checking for women with the smallest age - but the smallest age was from the whole population which may not exist, therefore you get no records returned.

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE sex= "female" AND age=(SELECT MIN(age) FROM mytable where sex='female') ;
QUIT;

Do you have to use a subquery? A HAVING clause is easier to code and understand.

 

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE sex= "female" 
having age=min(age) ;
QUIT;

@Anita_n wrote:

Dear all,

I have a table consisting of a list of females and males as follows:

Anita_n_0-1612302355318.png

I wish to use the below subquery to get out the first and last name in one column for

for the female with the minimum age. But am not getting it working. I will appreciate

any help. Thanks

this code works fine for the minimum age for both sex

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE age=(SELECT MIN(age) FROM mytable) ;
QUIT;

 but I want to add to this code, that the minimum age should not apply to both sex but only the females.

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE sex= "female" AND age=(SELECT MIN(age) FROM mytable) ;
QUIT;

if I add this part with sex="female" I get no results


 

View solution in original post

4 REPLIES 4
Reeza
Super User

Does this work for you? Your previous query was checking for women with the smallest age - but the smallest age was from the whole population which may not exist, therefore you get no records returned.

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE sex= "female" AND age=(SELECT MIN(age) FROM mytable where sex='female') ;
QUIT;

Do you have to use a subquery? A HAVING clause is easier to code and understand.

 

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE sex= "female" 
having age=min(age) ;
QUIT;

@Anita_n wrote:

Dear all,

I have a table consisting of a list of females and males as follows:

Anita_n_0-1612302355318.png

I wish to use the below subquery to get out the first and last name in one column for

for the female with the minimum age. But am not getting it working. I will appreciate

any help. Thanks

this code works fine for the minimum age for both sex

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE age=(SELECT MIN(age) FROM mytable) ;
QUIT;

 but I want to add to this code, that the minimum age should not apply to both sex but only the females.

PROC SQL;
SELECT lastname,firstname, age,
CATS(firstname||" "||lastname) AS Name FROM mytable
WHERE sex= "female" AND age=(SELECT MIN(age) FROM mytable) ;
QUIT;

if I add this part with sex="female" I get no results


 

Anita_n
Pyrite | Level 9
Okay thanks for the quick reply, I need to test that. I will leave a feedback
PGStats
Opal | Level 21

and you should replace

 

CATS(firstname||" "||lastname)

 

with

 

CATX(" ", firstname, lastname)

PG
Anita_n
Pyrite | Level 9
Thanks, it worked fine

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 722 views
  • 1 like
  • 3 in conversation