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

Hi,

 

Here is some data, where I need to fetch yymmdd10. format data (e.g. 2020-06-08).

which includes all Jan-Dec or January to December data with (- or / ) seperates.


data xyz;
date1= "08-June-2020";output;
date1= "31-Oct-2020";output;
date1= "13/August/2020"; output;
date1= "02/Jan/2020" ;output;

run;

 

Thank you in advance for your help and take care.

 

Regards

Priya

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@pdhokriya wrote:
3rd record is coming null. Not working 😞

Nope:

data xyz;
length date1 $30.;
date1= "08-June-2020";output;
date1= "31-Oct-2020";output;
date1= "13/August/2020"; output;
date1= "02/Jan/2020" ;output;
run;

data want;
   set xyz;
   
   newvar = input(date1, ANYDTDTE20.);
   format newvar yymmdd10.;
run;

proc print data=want noobs;
run;

Result:

date1	newvar
08-June-2020	2020-06-08
31-Oct-2020	2020-10-31
13/August/2020	2020-08-13
02/Jan/2020	2020-01-02

View solution in original post

11 REPLIES 11
andreas_lds
Jade | Level 19

Convert the strings to sas-dates, preferably during data-import. Then all you have to do is change the format to get what you want. If you can't fix the problem during import, then use a datastep and the input-function to create sas-dates.

pdhokriya
Pyrite | Level 9
Please share your program approch because this does not work for my data.


data xyz;
date1= "08-June-2020";output;
date1= "31-Oct-2020";output;
date1= "13/August/2020"; output;
date1= "02/Jan/2020" ;output;
run ;

data one;
set xyz;
newvar = input(date1, best.);
format newvar yymmdd10.;
run;
andreas_lds
Jade | Level 19

Try:

data xyz;
   length date1 $ 20;
   date1= "08-June-2020"; output;
   date1= "31-Oct-2020"; output;
   date1= "13/August/2020"; output;
   date1= "02/Jan/2020"; output;
run;

data want;
   set xyz;
   
   newvar = input(date1, ANYDTDTE20.);
   format newvar yymmdd10.;
run;
pdhokriya
Pyrite | Level 9
3rd record is coming null. Not working 😞
PaigeMiller
Diamond | Level 26

I don't get a null when I use the code from @andreas_lds .

 

Show us the exact code you are using, better yet show us the full complete unedited LOG from the code you ran, and the results. To show us the log, please preserve the formatting by following these instructions

--
Paige Miller
pdhokriya
Pyrite | Level 9
My approch is this:

Please share yours.

data xyz;
length date1 $30.;
date1= "08-June-2020";output;
date1= "31-Oct-2020";output;
date1= "13/August/2020"; output;
date1= "02/Jan/2020" ;output;
run ;


data one;
set xyz;
array char_month(24) $20 _temporary_ ("JANUARY" "FEBRUARY" "MARCH" "APRIL" "MAY" "JUNE" "JULY" "AUGUST" "SEPTEMBER" "OCTOBER" "NOVEMBER" "DECEMBER"
"JAN" "FEB" "MAR" "APR" "MAY" "JUN" "JUL" "AUG" "SEP" "OCT" "NOV" "DEC");
array num_month (24) $20 _temporary_ ("01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12"
"01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" );

do j = 1 to 24 ;
if strip(char_month(j)) = strip(upcase(scan (tranwrd (date1,"/","-"),2,"-")))
then cm_date=catx("-",scan (tranwrd (date1,"/","-"),3,"-"),num_month(j),scan (tranwrd (date1,"/","-"),1,"-"));
end;
drop j;
run;
Kurt_Bremser
Super User

@pdhokriya wrote:
3rd record is coming null. Not working 😞

Nope:

data xyz;
length date1 $30.;
date1= "08-June-2020";output;
date1= "31-Oct-2020";output;
date1= "13/August/2020"; output;
date1= "02/Jan/2020" ;output;
run;

data want;
   set xyz;
   
   newvar = input(date1, ANYDTDTE20.);
   format newvar yymmdd10.;
run;

proc print data=want noobs;
run;

Result:

date1	newvar
08-June-2020	2020-06-08
31-Oct-2020	2020-10-31
13/August/2020	2020-08-13
02/Jan/2020	2020-01-02
pdhokriya
Pyrite | Level 9
Thank you for your quick reply and help. My approch was long but your approch is simple, short and best, saved me time thanks again.
andreas_lds
Jade | Level 19

@pdhokriya wrote:
Thank you for your quick reply and help. My approch was long but your approch is simple, short and best, saved me time thanks again.

In fact the code you marked as solution is exactly the code i posted before.

pdhokriya
Pyrite | Level 9
Thank you for the solution..

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 11 replies
  • 8578 views
  • 2 likes
  • 4 in conversation