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

Hi,

I have a dataset:

ID    Sub_ID   Measure_year         Ht        Wt

1       12             06/06/1995            .           .

1       13             07/08/1997            .        135

1       14             07/29/1987          66          .

1       15             08/01/1988          56       140

 

I want the dataset:

where I need to select the Ht which has the first value in it based on the earliest Measure_year with values in Ht and Wt.

ID          Sub_ID        Measure_time           Ht     Wt

1              14               07/29/1987              66     135

 

Can I do this with more ID's?

thank in advance.

-Smitha

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Not sure what you are after, but you might just want to use the UPDATE statement for this.  Just use the same dataset with zero observations as the BASE dataset to treat all of the observations as TRANSACTIONS.

data want;
  update have(obs=0) have;
  by id;
run;

If you want the FIRST instead of the LAST then sort the input dataset by ID DESCENDING MEASURE_YEAR 

 

You could also try using the MERGE statement.

data ht;
  set have;
  where not missing(ht);
  by id;
  if first.id;
  keep id measure_year ht ;
  rename measure_year=ht_year;

run;
data wt;
  set have;
  where not missing(wt);
  by id;
  if first.id;
  keep id measure_year wt ;
  rename measure_year=wt_year;
run;

data want;
  merge ht wt;
  by id;
run;

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Not sure what you are after, but you might just want to use the UPDATE statement for this.  Just use the same dataset with zero observations as the BASE dataset to treat all of the observations as TRANSACTIONS.

data want;
  update have(obs=0) have;
  by id;
run;

If you want the FIRST instead of the LAST then sort the input dataset by ID DESCENDING MEASURE_YEAR 

 

You could also try using the MERGE statement.

data ht;
  set have;
  where not missing(ht);
  by id;
  if first.id;
  keep id measure_year ht ;
  rename measure_year=ht_year;

run;
data wt;
  set have;
  where not missing(wt);
  by id;
  if first.id;
  keep id measure_year wt ;
  rename measure_year=wt_year;
run;

data want;
  merge ht wt;
  by id;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 529 views
  • 1 like
  • 2 in conversation