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

I am trying to create a new variable called "stroke visit" using the information below. I have the date of stroke and visit numbers available. The stroke visit would be closest to the date of stroke (preferably before the latest visit) e.g. for ID 2 the visitnum should be "3".

 

id stroke dateofstrk   visitdate   visitnum

1    0               .          11/1/2022     1

1    0               .          12/1/2022     2

1    0               .          1/1/2023       3

2    1     12/25/2022  11/1/2022      1

2    1     12/25/2022  12/1/2022      2

2    1     12/25/2022  1/1/2023        3

3    1     11/13/2022  11/1/2022      1

3    1     11/13/2022  12/1/2022      2

3    1     11/13/2022  11/1/2023      3

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

The date of the first visit occuring at or after the date of stroke can be obtained as:

 

data have;
input id stroke (dateOfStrk visitDate) (:mmddyy.) visitNum;
format dateofStrk visitDate yymmdd10.;
datalines;
1    0               .          11/1/2022     1
1    0               .          12/1/2022     2
1    0               .          1/1/2023       3
2    1     12/25/2022  11/1/2022      1
2    1     12/25/2022  12/1/2022      2
2    1     12/25/2022  1/1/2023        3
3    1     11/13/2022  11/1/2022      1
3    1     11/13/2022  12/1/2022      2
3    1     11/13/2022  11/1/2023      3
;

proc sql;
/* create table want as */
select 
    a.*, b.strkVisit format=yymmdd10.
from 
    have as a left join
    (select id, min(visitDate) as strkVisit 
    from have where stroke and visitDate>=dateOfStrk 
    group by id) as b
    on a.id=b.id
    order by id, visitNum;
quit;

PGStats_0-1679417189191.png

 

PG

View solution in original post

1 REPLY 1
PGStats
Opal | Level 21

The date of the first visit occuring at or after the date of stroke can be obtained as:

 

data have;
input id stroke (dateOfStrk visitDate) (:mmddyy.) visitNum;
format dateofStrk visitDate yymmdd10.;
datalines;
1    0               .          11/1/2022     1
1    0               .          12/1/2022     2
1    0               .          1/1/2023       3
2    1     12/25/2022  11/1/2022      1
2    1     12/25/2022  12/1/2022      2
2    1     12/25/2022  1/1/2023        3
3    1     11/13/2022  11/1/2022      1
3    1     11/13/2022  12/1/2022      2
3    1     11/13/2022  11/1/2023      3
;

proc sql;
/* create table want as */
select 
    a.*, b.strkVisit format=yymmdd10.
from 
    have as a left join
    (select id, min(visitDate) as strkVisit 
    from have where stroke and visitDate>=dateOfStrk 
    group by id) as b
    on a.id=b.id
    order by id, visitNum;
quit;

PGStats_0-1679417189191.png

 

PG

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 111 views
  • 1 like
  • 2 in conversation