BookmarkSubscribeRSS Feed
gahlot1999
Fluorite | Level 6

Hi Community,

Few values are missing in my dataset. I need to put them in sequence as given below.

Source Dataset:

SubjectSequence
1011
101 
1012
1012
101 
101 
101 
1013
1013

Output Dataset:

SubjectSequence
1011
1011.1
1012
1012
1012.1
1012.2
1012.3
1013
1013

Thank you in advance..

4 REPLIES 4
mkeintz
PROC Star

This is a good case for using the RETAIN statement, applied to a temporarily needed" variable:

 

data have;
  infile datalines missover;
  input subject sequence;
datalines;
101	1
101	 
101	2
101	2
101	 
101	 
101	 
101	3
101	3
run;
data want (drop=_:);
  set have;
  retain _sequence;
  /* Replace by next line:  _sequence=coalesce(sequence,sum(_sequence,0.1));  */
  _sequence=round(coalesce(sequence,sum(_sequence,0.1)),0.1);
  sequence=_sequence;
run;

The coalesce function returns the first non-missing argument.

 

 

Editted note:  I incorporated a change to accommodate the precision issue noted by @FreelanceReinh.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
PaigeMiller
Diamond | Level 26

If there are 10 or more blank values consecutively for variable SEQUENCE, then what? Can this actually happen?

--
Paige Miller
Ksharp
Super User
/*You should know for numeric variable,
10.1 and 10.10 is the same thing*/
data have;
  infile datalines missover;
  input subject sequence;
datalines;
101 1
101  
101 2
101 2
101  
101  
101  
101 3
101 3
;
data want(drop=temp n);
  set have;
  by subject ;
  retain temp n;
  if first.subject then call missing(temp,n);
  if not missing(sequence) then do;temp=sequence;want=sequence;n=0;end;
   else do;n+1;want=sum(temp,n*0.1);end;
run;
FreelanceReinh
Jade | Level 19

Hi @gahlot1999,

 

Just a minor addition to the solutions you've already received:

 

If you want to ensure that the computed sequence numbers are equal to what they look like, i.e., if you want to avoid later "surprises" as in

168   data _null_;
169   if 1.1+0.1 ne 1.2 then put 'Surprised?';
170   if 1+7*0.1 ne 1.7 then put 'Surprised?';
171   run;

Surprised?
Surprised?
NOTE: DATA statement used (Total process time):

you should either round the final result, e.g.,

  sequence=round(_sequence,0.1);
want=round(sum(temp,n*0.1),0.1);

or work with integers as long as possible:

want=sum(temp*10,n)/10;

 

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
  • 4 replies
  • 502 views
  • 4 likes
  • 5 in conversation