BookmarkSubscribeRSS Feed
lerdem
Quartz | Level 8

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. 

 

       

6 REPLIES 6
Astounding
PROC Star

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;

lerdem
Quartz | Level 8

Thank you but it is not clear

Astounding
PROC Star

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?

lerdem
Quartz | Level 8

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

Reeza
Super User
What error do you receive? What code did you use? Saying you have an error isn't helpful for debugging.
MikeZdeb
Rhodochrosite | Level 12

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

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
  • 6 replies
  • 1462 views
  • 0 likes
  • 4 in conversation