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
@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
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.
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;
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
@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 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.
Please mark @andreas_lds 's post as solution, I only provided proof by adding PROC PRINT.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.