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

I have to assign numbers to groups in two ways. First I was able to achieve(pt_id). The other one, I want to assign the same number to each soc, so in this case, soc1 will have value 1 for all first 3 rows. And soc 2 will have value of 2.

 

data in;
input soc $ pt $;
datalines ;
soc1 pt1
soc1 pt2
soc1 pt8
soc2 pt1
;
run;

data out;
set in;
by soc pt;
if first.soc then pt_id=0;
pt_id+1;
run;

 

output, should look like this...

 

soc   pt      pt_id soc_id

____________________

soc1 pt1      1          1
soc1 pt2      2          1
soc1 pt8      3          1
soc2 pt1      1          2

 

What I should add in the datastep to get he soc_id.

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

please try the below code , only update is

 

if first.soc then soc_id+1;

 

data out;
set in;
by soc pt;
if first.soc then pt_id=0;
pt_id+1;
if first.soc then soc_id+1;

run;
Thanks,
Jag

View solution in original post

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

please try the below code , only update is

 

if first.soc then soc_id+1;

 

data out;
set in;
by soc pt;
if first.soc then pt_id=0;
pt_id+1;
if first.soc then soc_id+1;

run;
Thanks,
Jag
s_lassen
Meteorite | Level 14

This should do it:

data want;
  set in;
  by soc pt;
  if first.soc then do;
    pt_id=1;
    soc_id+1;
    end;
  else
    pt_id+1;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 3 replies
  • 2684 views
  • 3 likes
  • 4 in conversation