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. 🙂

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1544 views
  • 0 likes
  • 3 in conversation