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

i have a dataset like this.

I want to use the first value of weight for the respective subjects.someone pls help me.

        input                                 desired output

id  weight                              id        weight 

1    58                                    1        58

1     .                                      1        58

1     .                                      1        58

2    62                                    2        62

2     .                                      2        62

2     .                                      2        62

3    57                                    3        57

3     .                                      3        57

3    .                                       3        57

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
AKHILA
Obsidian | Level 7

it works. thank u so much

View solution in original post

7 REPLIES 7
singhsahab
Lapis Lazuli | Level 10

Hi Akhila,

 

i hope below code will help...

 

data have;
input id  weight;
cards;                          
1 58
1 .
1 . 
2 62
2 .
2 .
3 57
3 .
3 .
;
run;    

proc sort ; by id;
run;

data want;
set have;
by id;
retain i ;
if first.id then i=weight;
weight=i;
drop i;
run;

 

Thanks...

AKHILA
Obsidian | Level 7

it works. thank u so much

PeterClemmensen
Tourmaline | Level 20

@AKHILA, please mark @singhsahabs answer as the solution to your problem.

novinosrin
Tourmaline | Level 20
data have;
input id  weight;
cards;                          
1 58
1 .
1 . 
2 62
2 .
2 .
3 57
3 .
3 .
;
run;  

data want;
update have (obs=0) have;
by id;
output;
run;
novinosrin
Tourmaline | Level 20

data have;
input id  weight;
cards;                          
1 58
1 .
1 . 
2 62
2 .
2 .
3 57
3 .
3 .
;
run;  
proc sql;
create table want(drop=w) as
select *,max(w) as weight
from have(rename=weight=w)
group by id;
quit;
novinosrin
Tourmaline | Level 20
data have;
input id  weight;
cards;                          
1 58
1 .
1 . 
2 62
2 .
2 .
3 57
3 .
3 .
;
run;  
data want;
merge have(drop=weight) have(where=(weight > .));
by id;
run;
novinosrin
Tourmaline | Level 20

data have;
input id  weight;
cards;                          
1 58
1 .
1 . 
2 62
2 .
2 .
3 57
3 .
3 .
;
run; 

data want;
if _n_=1 then do;
 dcl hash H (dataset:'have(where=(weight > .))') ;
   h.definekey  ("id") ;
   h.definedata ("weight") ;
   h.definedone () ;
end;
set have;
_iorc_=h.find();
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 3355 views
  • 1 like
  • 4 in conversation