BookmarkSubscribeRSS Feed
pdhokriya
Pyrite | Level 9

/* My requirement final data in new variable should consist of blank records and less then "2020" , "2020-10" , "2020-10-09" */


data abc ;
date = "2020-10-09"; output ;
date = "2009"; output ;
date = "2020-08"; output ;
date = "2020-09"; output ;
date = "2020-10"; output ;
date = "2020-10-08"; output ;
date = ""; output ;
date = "2020-10-07"; output ;
date = "2020"; output ;
run;


/******This is my approch which I am not satisfied with.*******/


proc sql ;
create table ck as
select * from (select date,
case when length (date) = 4 and date < "2020" then date
when length (date) = 7 and date < "2020-10" then date
when length (date) = 10 and date < "2020-10-09" then date
when date = "" then ""
else "invalid"
end as xyz
from abc ) where xyz ne "invalid" ;
quit;

 

Thank you in advance.

 

Take care

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Please state why you are unhappy with the results. Show us the desired results.

--
Paige Miller
pdhokriya
Pyrite | Level 9
I am happy with result , i am not happy with my program.
RichardDeVen
Barite | Level 11

The question is a string comparison question and only involves strings that look like calendar values.

 

Perhaps this will help

proc sql ;
create table ck as
select * from (
  select date,
    case 
      when length (date) =  4 and date < "2020"       then "< 2020"
      when length (date) =  7 and date < "2020-10"    then "< 2020-10"
      when length (date) = 10 and date < "2020-10-09" then "< 2020-10-09"
      when date = "" then ""
      else "invalid"
    end as xyz
    from abc
) get_me_out_of_this_asylum
where xyz ne "invalid" ;
quit;

RichardADeVenezia_1-1606309963257.png

 

 

pdhokriya
Pyrite | Level 9
This is not desired output. Check my program output data.
RichardDeVen
Barite | Level 11

Why does the query have a sub-select ? Is that what you are unhappy with ?  What streamlined query do you intuitively feel could be written ?

pdhokriya
Pyrite | Level 9
I was looking without "invalid" data. filtering of invalid data which is annoying to me. 🙂

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
  • 6 replies
  • 663 views
  • 0 likes
  • 3 in conversation