BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Chung-Li
Quartz | Level 8

Hi guys, 

 

I've encountered a problem that I couldn't understand why couldn't be solved.

Please take a look at the following dataset: (ID and Level are what I have, while Sn is what I want to generate)

ID    Level    Sn

1         L        1

1         L        1

            .

            .

            .
1         M        2

1         M        2

            .

            .

            .

2         H        3

2         H        3

            .

            .

            .

2         L        4

2         L        4

            .

            .

            .
2         M        5

2         M        5

            .

            .

            .

2         H        6

2         H        6

            .

            .

            .

 

General rule is that every subject has several records on three levels, and what I want to do is to generate serial number according to different levels for each subject.

I know it can be achieved by the combination of "do loop" and "by" statement.

However, the work just couldn't be done. (still trying)

Therefore, I post it to ask you guys for some help!

 

Thanks in advance!

 

Best regards, 

Bill

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Try this:

data work.have;
   length id 8 level $ 1;
   input id level;
   datalines;
1 L
1 L
1 M
1 M
2 H
2 H
2 L
2 L
2 H
2 H
;
run;

data work.want;
   set work.have;
   length serial 8;
   retain serial 0;

   if level ^= lag(level) then do;
      serial = serial + 1;
   end;

run;

View solution in original post

2 REPLIES 2
andreas_lds
Jade | Level 19

Try this:

data work.have;
   length id 8 level $ 1;
   input id level;
   datalines;
1 L
1 L
1 M
1 M
2 H
2 H
2 L
2 L
2 H
2 H
;
run;

data work.want;
   set work.have;
   length serial 8;
   retain serial 0;

   if level ^= lag(level) then do;
      serial = serial + 1;
   end;

run;
Chung-Li
Quartz | Level 8

Thanks, andreas!

 

I guess the problem is on "retain..."

However, I don't know why it should exist if there's already "serial=serial+1" 

 

But still thanks, you spot my blind point.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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