DATA Step, Macro, Functions and more

if first. and last observations

Reply
Contributor
Posts: 47

if first. and last observations

i have table as below

    Type      course     crn       meetday     meettime                          NEW COLUMN

       2         MAT20    215        MW             09                                             

       4         MAT20    154        MW             08                                             . 

       4          ACG15   214        T                 12                                             1 

       4          ACG15   214        MW             12                                             2

 

This is an example table. I have first 5 variables 'Type, course, crn , meetday and meettime' I need to add 'new column'.

If course and crn are same , but meetday or meettime  different then new column should show the number. 

 

       

Super User
Posts: 5,498

Re: if first. and last observations

This should do it.  Assuming that you have already sorted your data in a way that groups all the course/crn combinations:

 

data want;

set have;

by course crn notsorted;

if first.crn then do;

   if last.crn then new_column=.;

   else new_column=1;

run;

else new_column + 1;

run;

Contributor
Posts: 47

Re: if first. and last observations

Posted in reply to Astounding

Thank you but it is not clear

Super User
Posts: 5,498

Re: if first. and last observations

Well, there are some programming techniques that you may need to know:

 

  • BY statement in a DATA step
  • NOTSORTED
  • varname + 1

Are you asking about those, or are you asking about sorting your data, or are you asking about something else entirely?

Contributor
Posts: 47

Re: if first. and last observations

Posted in reply to Astounding

no actually, when i use your code it doesn't work. gives error. Should i change somethink?

Super User
Posts: 19,771

Re: if first. and last observations

What error do you receive? What code did you use? Saying you have an error isn't helpful for debugging.
Valued Guide
Posts: 765

Re: if first. and last observations

Hi, another suggestion ...

 

data x;
input type course :$5. cm meetday:$2. meettime;
datalines;
2 MAT20  215  MW  09                                            
4 MAT20  154  MW  08                                           
4 ACG15  214  T   12                                           
4 ACG15  214  MW  12                                           
;
 
data y;
do new_column=1 by 1 until (last.cm);
   set x;
   by course cm notsorted;
   if  first.cm and last.cm then call missing (new_column);
   output;
end;
run;

 

data set y ...

 

       new_
Obs    column    type    course     cm    meetday    meettime

 1        .        2     MAT20     215      MW           9
 2        .        4     MAT20     154      MW           8
 3        1        4     ACG15     214      T           12
 4        2        4     ACG15     214      MW          12

Ask a Question
Discussion stats
  • 6 replies
  • 254 views
  • 0 likes
  • 4 in conversation