BookmarkSubscribeRSS Feed
Vahe_Mar
Obsidian | Level 7

Hi All,

I need merge two datas, and take right variable from second data. 

first one my main data , second one data which have 3 answers for every subject. how we can merge it using sql in sas or sas.

Example

DATA A                                                              DATA B

USUBJID DATE                                         USUBJID     STARTDATE    ENDDATE     EPOCH 

1.001        10/10/17                                   1.001           09/10/17             14/10/17        SCREENING

                                                                  2.001           15/10/17              15/12/17       TREATMENT

                                                                  3.001           20/12/17              10/12/18       FOLLOW-UP

 

So need to check where is my date and take EPOCh for SCREENING for this example.

 

THANKS

Thanks,
Vahe
8 REPLIES 8
Kurt_Bremser
Super User
select a.usubjid, a.date, b.epoch
from data_a a, data_b b
where a.usubjid = b.usubjid and b.startdate <= a.date <= b.enddate
;

In SAS proc sql

 

Vahe_Mar
Obsidian | Level 7
there is one problem for screening should be <= date< , for treatment <= date<=, and for follow up <date <=
Thanks,
Vahe
Vahe_Mar
Obsidian | Level 7

I think example is fine

Thanks,
Vahe
Vahe_Mar
Obsidian | Level 7

great

Thanks,
Vahe
Vahe_Mar
Obsidian | Level 7

I solved problem with this. 

 

*Derive EPOCH*;
proc sql noprint;
create table epoch as
select a.* ,b.epoch
from faf1 as a left join sds.se as b
on a.usubjid=b.usubjid and
(
(length(a.FADTC)=10 and length(b.SESTDTC)=10 and b.SESTDTC<=a.FADTC)
and
(length(a.FADTC)=10 and length(b.SEENDTC)=10 and a.FADTC<=b.SEENDTC)
and
TAETORD =2

or

(length(a.FADTC)=10 and length(b.SESTDTC)=10 and b.SESTDTC<=a.FADTC)
and
(length(a.FADTC)=10 and length(b.SEENDTC)=10 and a.FADTC<b.SEENDTC)
and
TAETORD =1
)
order by a.usubjid, a.facat, a.fascat, a.faobj
;
quit;

Thanks,
Vahe
mkeintz
PROC Star

@Vahe_Mar:

 

Your smug reply to @Kurt_Bremser in declining to provide a usable data set might be a little more sustainable if your solution below actually used variable names you provided in your original topic entry (also below). And there is nothing  in the original post  that would lead one to see a need for the LENGTH functions.

 

I'm glad you solved your problem, but it would be good for later viewers to see your proc sql code marked as the solution, and the problem description revised to make the solution apparent.

 

 

 

original message:

Hi All,

I need merge two datas, and take right variable from second data. 

first one my main data , second one data which have 3 answers for every subject. how we can merge it using sql in sas or sas.

Example

DATA A                                                              DATA B

USUBJID DATE                                         USUBJID     STARTDATE    ENDDATE     EPOCH 

1.001        10/10/17                                   1.001           09/10/17             14/10/17        SCREENING

                                                                  2.001           15/10/17              15/12/17       TREATMENT

                                                                  3.001           20/12/17              10/12/18       FOLLOW-UP

 

So need to check where is my date and take EPOCh for SCREENING for this example.

 

Your solution:

 

solved problem with this. 

 

*Derive EPOCH*;
proc sql noprint;
create table epoch as
select a.* ,b.epoch
from faf1 as a left join sds.se as b
on a.usubjid=b.usubjid and
(
(length(a.FADTC)=10 and length(b.SESTDTC)=10 and b.SESTDTC<=a.FADTC)
and
(length(a.FADTC)=10 and length(b.SEENDTC)=10 and a.FADTC<=b.SEENDTC)
and
TAETORD =2

or

(length(a.FADTC)=10 and length(b.SESTDTC)=10 and b.SESTDTC<=a.FADTC)
and
(length(a.FADTC)=10 and length(b.SEENDTC)=10 and a.FADTC<b.SEENDTC)
and
TAETORD =1
)
order by a.usubjid, a.facat, a.fascat, a.faobj
;
quit;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 8 replies
  • 5539 views
  • 2 likes
  • 3 in conversation