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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 494 views
  • 0 likes
  • 2 in conversation