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

Hi expert,

I had several datasets. Some datasets only contains thru_dt, and some datasets contains both from_dt and thru_dt. I wanted to select

a). if the dataset only contains thru_dt, then select '01JAN2015'd <=thru_dt<='05JAN2015'd,

b). if the dataset contains both from_dt and thru_dt, then select '01JAN2015'd <=from_dt<='05JAN2015'd.

 

For example:

data one;
input id $ thru_dt:mmddyy10.;
format thru_dt mmddyy10.;
datalines;
1 01/01/2015
2 01/02/2015
3 01/03/2015
4 01/04/2015
5 01/05/2015
6 01/06/2015
7 01/07/2015
;
run;

data two;
input id $ from_dt:mmddyy10. thru_dt:mmddyy10.;
format from_dt thru_dt mmddyy10.;
datalines;
1 01/01/2015 01/02/2015
2 01/02/2015 01/03/2015
3 01/03/2015 01/03/2015
4 01/04/2015 01/04/2015
5 01/05/2015 01/05/2015
6 01/06/2015 01/06/2015
7 01/07/2015 01/07/2015
;
run;

 

I tried the following codes, but it said ERROR: Variable from_dt is not on file WORK.ONE.

data want;
set one two;
where '01JAN2015'd <=from_dt<='05JAN2015'd | '01JAN2015'd <=thru_dt<='05JAN2015'd;
run;

 

Any suggestions would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Like this?

data WANT;
 set ONE TWO;
 where '01JAN2015'd <= coalesce(from_dt,thru_dt) <= '05JAN2015'd ;

run;

View solution in original post

1 REPLY 1
ChrisNZ
Tourmaline | Level 20

Like this?

data WANT;
 set ONE TWO;
 where '01JAN2015'd <= coalesce(from_dt,thru_dt) <= '05JAN2015'd ;

run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 526 views
  • 0 likes
  • 2 in conversation