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

Hello,

I would like to get the table on right hand side, from a table that is on the left hand side:

stataq_0-1697323489687.png

Basically, I want when `order`=.,  and then by `ID`, `order` will be last `order`+1. The red number will be the number I want.

What should I do in order to get such result.

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@stataq wrote:
It looks like this method will change current order 2& 3 to 2, and only have 6 instead 7. Any way to solve this?

That's why you should provide sample data via a SAS datastep so we can actually test the code we propose.

data have;
  infile datalines dsd dlm=',' truncover;
  input id $ order order_want;
  datalines;
B1,,1
B1,,1
,2,2
,2,2
,3,3
,3,3
,3,3
C1,,4
C1,,4
C1,,4
C3,,5
C3,,5
,6,6
,6,6
D4,,7
D4,,7
;
data want;
  set have(rename=(order=order_have));
  by id notsorted;
  if not missing(order_have) then order_derived=order_have;
  else if first.id then order_derived+1;
run;

proc print data=want;
run;

View solution in original post

3 REPLIES 3
Patrick
Opal | Level 21

Please provide sample data via a SAS data step that creates the data. 

If the Have data you shared is representative for your real data then below should work.

data want;
  set have(drop=order);
  by id notsorted;
  if first.id then order+1;
run;

proc print data=want;
run;

 

stataq
Quartz | Level 8
It looks like this method will change current order 2& 3 to 2, and only have 6 instead 7. Any way to solve this?
Patrick
Opal | Level 21

@stataq wrote:
It looks like this method will change current order 2& 3 to 2, and only have 6 instead 7. Any way to solve this?

That's why you should provide sample data via a SAS datastep so we can actually test the code we propose.

data have;
  infile datalines dsd dlm=',' truncover;
  input id $ order order_want;
  datalines;
B1,,1
B1,,1
,2,2
,2,2
,3,3
,3,3
,3,3
C1,,4
C1,,4
C1,,4
C3,,5
C3,,5
,6,6
,6,6
D4,,7
D4,,7
;
data want;
  set have(rename=(order=order_have));
  by id notsorted;
  if not missing(order_have) then order_derived=order_have;
  else if first.id then order_derived+1;
run;

proc print data=want;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

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