BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Q1983
Lapis Lazuli | Level 10


data have;
length catgy $25 month_ $5 tot_cnt 8 descr $25 descr_pct monthsum 8
tran_type $3 seq_id 8 ;
input catgy -- seq_id;
datalines;
CURRENT JAN20 33 STAYED 98 1 D 3
CURRENT JAN20 33 ROLLED 2 1 D 3
CURRENT FEB20 25 STAYED 2 2 D 3
CURRENT FEB20 25 ROLLED 4 2 D 2
;
run;

Produces the following                  
                       
catgy month_ tot_cnt descr descr_pct monthsum tran_type seq_id        
CURRENT JAN20 33 STAYED 98 1 D 3        
CURRENT JAN20 33 ROLLED 2 1 D 3        
CURRENT FEB20 25 STAYED 2 2 D 3        
CURRENT FEB20 25 ROLLED 4 2 D 2        
                       
Desired result is to add a new row, place an F under tran_type and place the total count of the records under seq_id
                       
catgy month_ tot_cnt descr descr_pct monthsum tran_type seq_id        
CURRENT JAN20 33 STAYED 98 1 D 3        
CURRENT JAN20 33 ROLLED 2 1 D 3        
CURRENT FEB20 25 STAYED 2 2 D 3        
CURRENT FEB20 25 ROLLED 4 2 D 2        
            F 4        
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This works for your example:

data want;
   set have end=last curobs=obsnum;
   output;
   if last then do;
      call missing (of catgy-- monthsum);
      seq_id=obsnum ;
      Tran_type='F';
      output;
   end;
run;

End option creates a temporary variable that is set to 1 (true) when the current observation is last.

The Curobs option creates a temporary variable with the observation number, i.e. the row number in the data.

Call Missing, if you have not encountered it, sets a list of variables to missing values. It is one of the few functions that works on both character and numeric values.

View solution in original post

2 REPLIES 2
ballardw
Super User

This works for your example:

data want;
   set have end=last curobs=obsnum;
   output;
   if last then do;
      call missing (of catgy-- monthsum);
      seq_id=obsnum ;
      Tran_type='F';
      output;
   end;
run;

End option creates a temporary variable that is set to 1 (true) when the current observation is last.

The Curobs option creates a temporary variable with the observation number, i.e. the row number in the data.

Call Missing, if you have not encountered it, sets a list of variables to missing values. It is one of the few functions that works on both character and numeric values.

Q1983
Lapis Lazuli | Level 10
Thanks, I can use this in similar code. Appreciate this

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 2 replies
  • 320 views
  • 1 like
  • 2 in conversation