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


Hi,

I have this data set: 1 parent with 2 children, both children appears on each case. case nr 2 repeat itself three tims for child nr 465. I need to create a variable (counter) which counts the children by the parent-ID. The expected results are dispayed on the right column "wanted counter". Can anyone sujest how to solve it? thanks.

obsCASEID-parentID-childWanted counter

1

1101111
21104552
32101111
4210

455

2
52104552
62104552
1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

Give the data as is, and with the assumption that the data is sorted or at least clustered:

data have;

input CASE    ID_parent    ID_child ;

cards;

1    10    111   

1    10    455   

2    10    111   

2    10    455   

2    10    455   

2    10    455   

;

data want;

set have;

by case id_parent Id_child notsorted;

if first.case then call missing (count);

   count+first.id_child;

run;

proc print;run;

Haikuo

View solution in original post

3 REPLIES 3
Haikuo
Onyx | Level 15

Give the data as is, and with the assumption that the data is sorted or at least clustered:

data have;

input CASE    ID_parent    ID_child ;

cards;

1    10    111   

1    10    455   

2    10    111   

2    10    455   

2    10    455   

2    10    455   

;

data want;

set have;

by case id_parent Id_child notsorted;

if first.case then call missing (count);

   count+first.id_child;

run;

proc print;run;

Haikuo

MCB
Calcite | Level 5 MCB
Calcite | Level 5

Thanks! I will try it!

DanielSantos
Barite | Level 11

Using lag, and deriving from Mike's simplified code here: https://communities.sas.com/message/137090#137090


data WANT;

set HAVE;

    retain WANTED;

    WANTED=sum(WANTED*(lag(CASE) = CASE),(lag(ID_child) ^= ID_child));

run;


More on the LAG function here:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a000212547.htm


Cheers from Portugal.

Daniel Santos @ www.cgd.pt

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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