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

Hello SAS experts,

Not sure if something like this is possible: You can see what this code is supposed to do: it would issue “over3M” and “over6M” if time (in days) between todays day and FUP_DUE_date is over 90 and 180 days, respectively.

My question is if it possible somehow to have, instead of “over3M” and “over6M”, the date given under FUP_DUE_date for the records that have “over3M” and “over3M” while, as given, for blank fields it should stay blank (second table is what I would need to obtain)

 

create table Table1 as

select Subject, Max(Datepart(Date)) as Last_FUP_date format=mmddyy10.,

intnx('day', calculated Last_FUP_date, 365) as FUP_DUE_date format=mmddyy10., today()- calculated FUP_DUE_date as Date_difference,

case when (calculated Date_difference>=90) then "over3M" else "" end as Over_3M, case when (calculated Date_difference>= 180) then "over6M" else "" end as Over_6M

from mydata.InitialTable

group by Subject;

quit;

 

SubjectLast_FUP_dateFUP_DUE_dateDate_differenceOver_3MOver_6M
Subject110/5/201910/4/2020668over3Mover6M
Subject23/22/20213/22/2022134over3M 
Subject35/3/20225/3/2023-273  
Subject48/6/20218/6/2022-3  
Subject51/14/20191/14/2020932over3Mover6M
Subject611/18/202111/18/2022-107  
Subject712/15/202112/15/2022-134  
Subject811/6/202011/6/2021270over3Mover6M

 

Need to get this:

SubjectLast_FUP_dateFUP_DUE_dateDate_differenceOver_3MOver_6M
Subject110/5/201910/4/202066810/4/202010/4/2020
Subject23/22/20213/22/20221343/22/2022 
Subject35/3/20225/3/2023-273  
Subject48/6/20218/6/2022-3  
Subject51/14/20191/14/20209321/14/20201/14/2020
Subject611/18/202111/18/2022-107  
Subject712/15/202112/15/2022-134  
Subject811/6/202011/6/202127011/6/202111/6/2021

 

Thank you very much for your valuable time.

Sincerely,

Batta

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Formats aren't copied over, so you need to specify the format on those columns as well.

case when (calculated Date_difference>=90) then calculated FUP_DUE_date else . end as Over_3M format = ddmmyy10.,

View solution in original post

13 REPLIES 13
PaigeMiller
Diamond | Level 26

Something like this:

 

case when (calculated Date_difference>=90) then FUP_DUE_date else . end as Over_3M
--
Paige Miller
Batta
Obsidian | Level 7
Thank you!
I've already tried that, also I tried entering calculated FUP_DUE_date as this variable is actually calculated within this step, but none of this options worked.

Thank you for trying!
PaigeMiller
Diamond | Level 26

@Batta wrote:
Thank you!
I've already tried that, also I tried entering calculated FUP_DUE_date as this variable is actually calculated within this step, but none of this options worked.

Thank you for trying!

Please don't ever tell us something didn't work and provide no other information. We can't help you when that's all the information you give us.

 

Show us the code. Show us the log. Show us the output. Give us information that we can use to help you.

--
Paige Miller
Batta
Obsidian | Level 7
My apology, I didn't post the output as I've tried similar approach before even posted this and it didn't work before as it's not working now, here is a new code based on suggestion by PaigeMiller and the output. Please let me know if I am making a mistake I am not aware of while typing:
 
the code:

 

create table Table1 as

select Subject, Max(Datepart(Date)) as Last_FUP_date format=mmddyy10.,

intnx('day', calculated Last_FUP_date, 365) as FUP_DUE_date format=mmddyy10., today()- calculated FUP_DUE_date as Date_difference,

case when (calculated Date_difference>=90) then FUP_DUE_date else . end as Over_3M,

case when (calculated Date_difference>= 180) then FUP_DUE_date else . end as Over_6M  

from mydata.InitialTable

group by Subject;

quit;

 

the output:
 
NOTE: Data file XXXX.DATA is in a format that is native to another host, or the
      file encoding does not match the session encoding. Cross Environment Data Access will be
      used, which might require additional CPU resources and might reduce performance.
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: The following columns were not found in the contributing tables: FUP_DUE_date.
Reeza
Super User
 

create table Table1 as

select Subject, Max(Datepart(Date)) as Last_FUP_date format=mmddyy10.,

intnx('day', calculated Last_FUP_date, 365) as FUP_DUE_date format=mmddyy10., 
today()- calculated FUP_DUE_date as Date_difference,

case when (calculated Date_difference>=90) then calculated FUP_DUE_date else . end as Over_3M,

case when (calculated Date_difference>= 180) then calculated FUP_DUE_date else . end as Over_6M  

from mydata.InitialTable

group by Subject;

quit;

I think you forgot the keyword calculated in front of FUP_DUE_DATE.

 

Try the above and post the log if it doesn't work. 

Batta
Obsidian | Level 7

Hi Reeza, I have tried that and I posted the what I got, but just to include that in this reply, here it is what I got after running the code you suggested:

 

Subject Last_FUP_date FUP_DUE_date Date_difference Over_3M Over_6M
Subject1 2/5/2021 2/5/2022 180 22681 22681
Subject2 5/16/2022 5/16/2023 -285    
Subject3 9/9/2021 9/9/2022 -36    
Subject4 1/28/2022 1/28/2023 -177    
Subject5 8/28/2019 8/27/2020 707 22154 22154
Subject6 11/6/2017 11/6/2018 1367 21494 21494

 

So I guess this numeric values should be converted back to dates... Not sure how to do that.

Thank you.

PaigeMiller
Diamond | Level 26

@Batta wrote:
My apology, I didn't post the output as I've tried similar approach before even posted this and it didn't work before as it's not working now, here is a new code based on suggestion by PaigeMiller and the output. Please let me know if I am making a mistake I am not aware of while typing:
 
the code:

 

create table Table1 as

select Subject, Max(Datepart(Date)) as Last_FUP_date format=mmddyy10.,

intnx('day', calculated Last_FUP_date, 365) as FUP_DUE_date format=mmddyy10., today()- calculated FUP_DUE_date as Date_difference,

case when (calculated Date_difference>=90) then FUP_DUE_date else . end as Over_3M,

case when (calculated Date_difference>= 180) then FUP_DUE_date else . end as Over_6M  

from mydata.InitialTable

group by Subject;

quit;

 

the output:
 
NOTE: Data file XXXX.DATA is in a format that is native to another host, or the
      file encoding does not match the session encoding. Cross Environment Data Access will be
      used, which might require additional CPU resources and might reduce performance.
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: The following columns were not found in the contributing tables: FUP_DUE_date.

Just so there's no confusion in the future, we want the entire PROC SQL code, not the parts that you select; and the entire log for PROC SQL, not the parts that you select to show us. That's from now on, 100% of the time, no exceptions, you need to show us entire PROCs and entire log for the PROC.

--
Paige Miller
Batta
Obsidian | Level 7

Or if I use this code: 

 

proc sql;

create table Table1 as

select Subject, Max(Datepart(Date)) as Last_FUP_date format=mmddyy10.,

intnx('day', calculated Last_FUP_date, 365) as FUP_DUE_date format=mmddyy10., today()- calculated FUP_DUE_date as Date_difference,

case when (calculated Date_difference>=90) then calculated FUP_DUE_date else . end as Over_3M,

case when (calculated Date_difference>= 180) then calculated FUP_DUE_date else . end as Over_6M  

from mydata.InitialTable

group by Subject;

quit;

 

I would get the following: 

Subject Last_FUP_date FUP_DUE_date Date_difference Over_3M Over_6M
Subject1 2/5/2021 2/5/2022 180 22681 22681
Subject2 5/16/2022 5/16/2023 -285    
Subject3 9/9/2021 9/9/2022 -36    
Subject4 1/28/2022 1/28/2023 -177    
Subject5 8/28/2019 8/27/2020 707 22154 22154
Subject6 11/6/2017 11/6/2018 1367 21494 21494

 

Meaning that those numeric values for dates (not sure why I am obtaining them instead of dates as the original value format under the column FUP_DUE_date is date. 

Any suggestions?

Thank you very much. 

 

Reeza
Super User

Formats aren't copied over, so you need to specify the format on those columns as well.

case when (calculated Date_difference>=90) then calculated FUP_DUE_date else . end as Over_3M format = ddmmyy10.,
Batta
Obsidian | Level 7

THAT'S IT! 

Thank you so much Reeza,

I didn't know that the format would not get copied... 

 

Much appreciated!

Batta

ballardw
Super User

SAS dates are number of days from 1Jan1960. So apply a date format to any result that should be a date if you want something to be human readable.  Just because you set a format for the Last_FUP_date does not mean that a different variable receiving the value will use the same format.

 


@Batta wrote:

Or if I use this code: 

 

proc sql;

create table Table1 as

select Subject, Max(Datepart(Date)) as Last_FUP_date format=mmddyy10.,

intnx('day', calculated Last_FUP_date, 365) as FUP_DUE_date format=mmddyy10., today()- calculated FUP_DUE_date as Date_difference,

case when (calculated Date_difference>=90) then calculated FUP_DUE_date else . end as Over_3M,

case when (calculated Date_difference>= 180) then calculated FUP_DUE_date else . end as Over_6M  

from mydata.InitialTable

group by Subject;

quit;

 

I would get the following: 

Subject Last_FUP_date FUP_DUE_date Date_difference Over_3M Over_6M
Subject1 2/5/2021 2/5/2022 180 22681 22681
Subject2 5/16/2022 5/16/2023 -285    
Subject3 9/9/2021 9/9/2022 -36    
Subject4 1/28/2022 1/28/2023 -177    
Subject5 8/28/2019 8/27/2020 707 22154 22154
Subject6 11/6/2017 11/6/2018 1367 21494 21494

 

Meaning that those numeric values for dates (not sure why I am obtaining them instead of dates as the original value format under the column FUP_DUE_date is date. 

Any suggestions?

Thank you very much. 

 


 

Batta
Obsidian | Level 7
Acknowledged!
Thank you.
ballardw
Super User

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "</>" icon or attached as text to show exactly what you have and that we can test code against.

 

At least describe what you mean by "none of this (sic) options worked". Did your variable have a number? Then likely need to assign a format to the value. No values? Something.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 13 replies
  • 1243 views
  • 0 likes
  • 4 in conversation