BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
user40
Calcite | Level 5

Hi 

 

I tried to make an index that assign number 1 for each sequence start in the table below. I try this with the following code: 

 

code.JPG

 

utskrift av tabell.JPG

But the problem is that i Want to have a new unique number for index when the sequence variable (seqn) start in the beginning again. Like 1 for seqn 1 , 2, and index=2 for seqn 1 until 14 in this example? 

 

How can I solve this example? 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

I do understand.

 

Here are my results from the code above. I think that is what you describe?

 

ID   seqn  index
101  1     1
101  2     1
101  3     1
101  1     2
101  2     2
102  1     1
102  2     1
102  3     1
102  4     1
102  1     2
102  2     2

View solution in original post

8 REPLIES 8
ballardw
Super User

I can't tell what you want the output to look like.

 

Note: we can't code against pictures.

It is best to provide data in the form of a data step. There are hundreds of examples on the forum for such.

PeterClemmensen
Tourmaline | Level 20

I think this is what you want

 

data have;
   do seqnr = 1, 2, 1 to 14;
      output;
   end;
run;

data want;
   set have;
   if seqnr = 1 then index + 1;
run;
user40
Calcite | Level 5
Hi

My problem that I have many individuals in this file and this indicator must be done for each individ. How can I do that?
PeterClemmensen
Tourmaline | Level 20

Do the individuals have IDs? As I understand it, you want to reset the index to 1 whenever seqnr is 1. Which is that my code does.

user40
Calcite | Level 5
Yes, but my dataset have first id number that are for example like this

ID, seqn
101, 1
101 ,2
101 ,3
101 ,1
101 ,2
102 ,1
102 ,2
102 ,3
102 ,4
102 ,1
102 ,2


Then I need this that the index asign 1 for ID number 1 from seqn 1 to 3 and index = 2 for 1,2 within the same id number. And it will start again on the next id-number and do it on the same way. How can i reset the index to 1 in the beginning of each id number in sas?
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input ID seqn;
datalines;
101 1
101 2
101 3
101 1
101 2
102 1
102 2
102 3
102 4
102 1
102 2
;

data want;
   set have;
   by ID;
   if first.ID then index = 0;
   if seqn = 1 then index + 1;
run;
user40
Calcite | Level 5
I only got index =1 in when seqn is 1 for this solution. I need also that index =1 for all 101 when seqn go from 1-3 , and index =2 when 1-2 in the next sequence in the same id number if you understand.
PeterClemmensen
Tourmaline | Level 20

I do understand.

 

Here are my results from the code above. I think that is what you describe?

 

ID   seqn  index
101  1     1
101  2     1
101  3     1
101  1     2
101  2     2
102  1     1
102  2     1
102  3     1
102  4     1
102  1     2
102  2     2

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
  • 8 replies
  • 495 views
  • 0 likes
  • 3 in conversation