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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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