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

Hello

 

I need to translate this STATA code into SAS: bys cpr protokol (dt_registrering): gen index1 = _n if year(dt_vurdering)< $year

 

A subset of my dataset looks like this:

 

cpr       protokol      dt_registration        dt_assesment

100      fys               05feb2016             21jan2016

100      fys               27apr2017             17jan2017

100      fys               27apr2018             17jan2018

100      ergo            23feb2016              10jan2016

100      ergo            19jan2017               5jan2017

100      ergo            19jan2018               5jan2018

200     fys                23feb2016              10jan2016

200     fys                24apr2017              15jan2017

200     ergo             19mar2016              7jan2016

200     ergo             20mar2017               12jan2017

 

I want to make  a new variable that counts the number of assesments before year 2018 group by CPR and Protokol.

 

Something like this:

 

cpr       protokol      dt_registration        dt_assesment   index1

100      fys               05feb2016             21jan2016          1

100      fys               27apr2017             17jan2017          2

100      fys               27apr2018             17jan2018          .

100      ergo            23feb2016              10jan2016         1

100      ergo            19jan2017               5jan2017           2

100      ergo            19jan2018               5jan2018           .

200     fys                23feb2016              10jan2016         1

200     fys                24apr2017              15jan2017         2

200     ergo             19mar2016              7jan2016          1

200     ergo             20mar2017               12jan2017        2

 

Thank you

 

Kind regards Frank

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @frakje   See whether my understanding is correct using the below. Thanks!

data have;
input cpr       protokol $     (dt_registration        dt_assesment) (:date9.);
format dt_registration        dt_assesment date9.;
cards;
100      fys               05feb2016             21jan2016
100      fys               27apr2017             17jan2017
100      fys               27apr2018             17jan2018
100      ergo            23feb2016              10jan2016
100      ergo            19jan2017               5jan2017
100      ergo            19jan2018               5jan2018
200     fys                23feb2016              10jan2016
200     fys                24apr2017              15jan2017
200     ergo             19mar2016              7jan2016
200     ergo             20mar2017               12jan2017
;
data want;
 set have;
 by cpr protokol notsorted;
 if first.protokol then count=1;
 else if year(dt_registration)<2018 then count+1;
 else count=.;
run;

 /*Or a safer bet below*/

data want;
 set have;
 by cpr protokol notsorted;
 if first.protokol then count=0;
 if year(dt_registration)<2018 then count+1;
 else count=.;
run;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @frakje   See whether my understanding is correct using the below. Thanks!

data have;
input cpr       protokol $     (dt_registration        dt_assesment) (:date9.);
format dt_registration        dt_assesment date9.;
cards;
100      fys               05feb2016             21jan2016
100      fys               27apr2017             17jan2017
100      fys               27apr2018             17jan2018
100      ergo            23feb2016              10jan2016
100      ergo            19jan2017               5jan2017
100      ergo            19jan2018               5jan2018
200     fys                23feb2016              10jan2016
200     fys                24apr2017              15jan2017
200     ergo             19mar2016              7jan2016
200     ergo             20mar2017               12jan2017
;
data want;
 set have;
 by cpr protokol notsorted;
 if first.protokol then count=1;
 else if year(dt_registration)<2018 then count+1;
 else count=.;
run;

 /*Or a safer bet below*/

data want;
 set have;
 by cpr protokol notsorted;
 if first.protokol then count=0;
 if year(dt_registration)<2018 then count+1;
 else count=.;
run;
tomrvincent
Rhodochrosite | Level 12
What have you tried so far?

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 661 views
  • 0 likes
  • 3 in conversation