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

Hello Experts,

 

My data is  LB_DOSS that have the values, for example, 

blbla 21/07/2021

blabla 23/06/2009

 

I extract the data like  Date_prelevement=scan(LB_DOSS,-1); My results are : 

 

21/07/2021

23/06/2009

 

Do you know please the best way to convert the date (DD/MM/YYYY) from character to numeric values  ?

 

Thank you for your help !

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

The SAS Docu for the SCAN() Function will tell you that this function allows for additional parameters of which one is for definition of the characters used as word separators.

Patrick_1-1646734829156.png

 

data want;
  infile datalines truncover;
  input LB_DOSS $40.;
  Date_prelevement=input(scan(LB_DOSS,-1,' '),ddmmyy10.);
  format date_prelevement date9.;
  datalines;
blbla 21/07/2021
blabla 23/06/2009
;

proc print data=want;
run;

Patrick_0-1646734664173.png

 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Can you please explain how we can find, in general, the date information you want in this character string? Do we find the first space after blablabla and read the date? Can blablabla have its own space in it? Do we have to follow some other rules? If so, what are they? We need to know what rules will work here. We shouldn't have to guess ... you know what the rules are so tell us.

 

As I said in your other thread, you are strongly advised to present data in an accepted and useful format without us even asking. Do this automatically from now on please. No exceptions, no excuses.

 

We need to see a portion of data set DOSS2 provided according to these instructions. Many people just ignore the instructions when I mention it. Please do not provide the data in any other manner, as that won't help, and we'll just ask you to follow the instructions again.

--
Paige Miller
SASdevAnneMarie
Barite | Level 11
Thank you for your message.
Sorry, I improved my question.
Tom
Super User Tom
Super User

The INPUT() function let's you use an INFORMAT to convert text into values.  So if you use the informat of DDMMYY it will be able to convert those string into date values.  You can then assign the values to a numeric variable and attach a format that works with date values to display them in a way that humans will understand.  I would avoid using DDMMYY or MMDDYY as the display format as either one will confuse half of your audience.  So use DATE or YYMMDD instead.

data want;
  set have;
  Date_prelevement=input(scan(LB_DOSS,-1),ddmmyy10.);
  format date_prelevement date9.;
run;
SASdevAnneMarie
Barite | Level 11
Hello Tom,

Finally, I do : Date_prelevement=scan(LB_DOSS,-3)||"/"||scan(LB_DOSS,-2)||"/"||scan(LB_DOSS,-1)
because when I do scan(LB_DOSS,-1) I have only the year. Do you know some option for scan function to avoid the "/" and get the date without scanning 3 times please ?

Thank you !
Patrick
Opal | Level 21

The SAS Docu for the SCAN() Function will tell you that this function allows for additional parameters of which one is for definition of the characters used as word separators.

Patrick_1-1646734829156.png

 

data want;
  infile datalines truncover;
  input LB_DOSS $40.;
  Date_prelevement=input(scan(LB_DOSS,-1,' '),ddmmyy10.);
  format date_prelevement date9.;
  datalines;
blbla 21/07/2021
blabla 23/06/2009
;

proc print data=want;
run;

Patrick_0-1646734664173.png

 

SASdevAnneMarie
Barite | Level 11
Thank you Patrick !

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 925 views
  • 3 likes
  • 4 in conversation