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!
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;
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;
See this:
data out;
set in;
by soc pt;
if first.soc
then do;
soc_id + 1;
pt_id = 1;
end;
else if first.pt then pt_id + 1;
run;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.