BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASSLICK001
Obsidian | Level 7

Hi I have the following 

 

id1 id2

01  1

01  1

01  2

02  1

02  1

02  1

 

How can I ideintify the different value id2 within id1?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

For that result:

 

proc sort data=have;

by id1 id2;

run;

 

data want;

set have;

by id1 id2;

if first.id1 then flag=1;

else if first.id2 then flag + 1;

run;

View solution in original post

6 REPLIES 6
Astounding
PROC Star

This both identifies and counts for you:

 

proc freq data=have;

tables id1 * id2 / list missing;

run;

SASSLICK001
Obsidian | Level 7
I wanted to use first. and last.
ballardw
Super User

@SASSLICK001 wrote:
I wanted to use first. and last.

It would help to show what you expect for output.

 

Maybe something like ?

data want;
   set have;
   by id1 id2;
   if first.id2;
run;
SASSLICK001
Obsidian | Level 7

I want to create a flag where the value changes per change in value for id2

 

 expected output

 

id1 id2  flag

01  x      1

01  x      1 

01  y      2

02  z      1

02  z      1

02  z      1

ballardw
Super User

Assuming your data is sorted by ID1 and ID2 as implied by your example:

data have;
input id1 $ id2  $;
datalines;
01  x 
01  x 
01  y 
02  z 
02  z 
02  z 
;
run;
data want;
   set have;
   by id1 id2;
   if first.id1 then flag=0;
   else flag = first.id2;
run;

I prefer the 0/1 coding as 0 means 'not a change' in this case and there are some advantages that I can count the number of changes in id1 by getting a sum of the flag variable.

 

 

If your data is not sorted then you need to provide a more realistic example of the data.

Astounding
PROC Star

For that result:

 

proc sort data=have;

by id1 id2;

run;

 

data want;

set have;

by id1 id2;

if first.id1 then flag=1;

else if first.id2 then flag + 1;

run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1476 views
  • 0 likes
  • 3 in conversation