How to make my output to read as yes or no? - sas

I want to display my data as either yes or no in the output for initaltesting, site visit, and follow up, how would I do that? There are numeric values for this on the data set but want character responses of "y" or "n"
PROC FORMAT;
VALUE SiteVisitfmt 1 = 'yes'
0 = 'no';
VALUE InitialTestingfmt 1 = 'yes'
2 = 'no';
VALUE TestEventfmt 1 = 'One Event '
2 = 'Two Events'
3 = 'Three Events'
4 = 'Four Events'
5 = 'Five Events';
VALUE FollowUpfmt 1 = 'yes'
0 = 'no';
FORMAT SiteVisit SiteVisitfmt. InitialTesting InitialTestingfmt. TestEvent TestEventfmt.
FollowUp FollowUpfmt.;
RUN;
data PMdataedits;
set PMdata (rename = (Number_of_Days_from_Onset_to_Sit =SiteVisit
Number_of_Days_between_Onset_and = InitialTesting
Number_of_Test_Events_in_IRIS = TestEvent
Number_of_Days_between_Test_1_an = FollowUp));
drop SPA;
attrib date1 format=date9.;
date1=input(date,mmddyy10.);
NewSiteVisit = put(SiteVisit, 8.);
NewInitialTesting = put(InitialTesting, 8.);
NewFollowUp = put(FollowUp, 8.);
NewSiteVisit=;
if (NewSiteVisit=<1) THEN NewSiteVisit= '1';
if (NewSiteVisit>1) THEN NewSiteVist= '0';
NewInitialTesting=;
if (NewInitialTesting<=2) THEN NewInitialTesting= '1';
if (NewInitialTesting>2) THEN NewInitialTesting='0';

This statement:
FORMAT SiteVisit SiteVisitfmt. InitialTesting InitialTestingfmt. TestEvent TestEventfmt.
FollowUp FollowUpfmt.;
Needs to be on the data step (sometime after data PMdataedits; but before the run; that you don't show), not in the proc format. That's the statement that assigns the format to a variable; each dataset (which is defined by a data step) has its own, unique set of variables that can be the same name as other datasets but have different contents and formats.
Also note that you don't have to name the formats after the variables, and don't need three different yes/no formats. You could have done:
proc format;
format ynf
'1'='yes'
'0'='no'
;
run;
And then used
format sitevisit initialtesting followup ynf.;
And that would have covered all three of them with one format. But what you did is legal, it's just more typing than you need!

Related

NOT+IN SAS operators combined, is this valid? Can't find documentation

I'm trying to understand how to code something along the lines of "NOT IN the LIST" type of logic in SAS.
I figured I could do "NOT" + "IN" as something like below.
Data work.OUT;
Set work.IN;
If VAR=1 then OUTPUT=1;
else if VAR=2 then OUTPUT=2;
else if VAR NOT in (1,2) then OUTPUT=3;
else OUTPUT=4;
run;
When I export the dataset all I see is OUTPUT=3 for all records. So something is happening in the derivation and it's transforming all VAR values into OUTPUT 3 values for some reason. Even though I know for a fact that other values exist in the VAR.
I don't understand what the problem is? Can we not combine NOT+IN operators? Alternatively, do you have any other ways of coding this type of logic in SAS? I rather not code each bit of code since I have more than 300 unique values for VAR
Welcome to Stack Overflow Alejandro. Your code assigns values 1 2 or 3 depending on what values are in the variable called var:
data in;
do var = 1 to 5;
output;
end;
run;
Data work.OUT;
set work.IN;
If VAR=1 then OUTPUT=1;
else if VAR=2 then OUTPUT=2;
else if VAR NOT in (1,2) then OUTPUT=3;
else OUTPUT=4;
run;
Your code says check for var = 1 then check for var = 2 and then check if it is not 1 or 2. The final else is never checked because a var will be 1 or 2 or not 1 or 2.
If you have a pile of if checks, you can use a select/when/otherwise/end block. It will check a series of rules (in the order you type them) and then will do something based on whichever rule is true first.
data out;
set in;
select;
when(var = 1) output = 1;
when(var = 2) output = 2;
when(var < 5) output = 3;
when(.) output = -9999999;
otherwise output = 42;
end;
run;
I hope that helps. If not please send up another flare.

How to format dates for use in SAS?

I am trying to adapt Method 4 in this paper to calculate the duration of many observations, but discounting overlapping dates: https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/048-31.pdf
For example, two rows of observations for subject 101 lasting from 2017-03-02 to 2017-03-16 and 2017-03-04 to 2017-03-17 respectively should return a value of only 16 days.
I am getting an error with the dates being 'Invalid numeric data', though, resulting in later errors. I have tried format startdate yyyymmdd10.; and format stopdate yyyymmdd10.; with no success.
Can anyone help me properly format my dates for use here, or identify any further errors?
Edit: Line 80 refers to do xdate = startdate to stopdate;.
I am still unable to convert or create the date variables as numeric/date values. I have used the following code:
data sasuser.Mdm;
set sasuser.Mdm;
do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
put xdate= yymmdd10.;
output;
end;
run;
To get this output:
1 data sasuser.Mdm;
2 set sasuser.Mdm;
3 do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
4 put xdate= yymmdd10.;
5 output;
6 end;
7 run;
xdate=2017-03-02
xdate=2017-03-03
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-17
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-03-09
xdate=2017-03-10
xdate=2017-03-11
xdate=2017-03-12
xdate=2017-03-13
xdate=2017-03-14
xdate=2017-03-15
xdate=2017-03-16
xdate=2017-03-17
xdate=2017-03-18
xdate=2017-03-19
xdate=2017-03-20
xdate=2017-03-21
xdate=2017-02-08
xdate=2017-02-09
xdate=2017-02-10
xdate=2017-02-11
xdate=2017-02-12
xdate=2017-02-13
xdate=2017-02-14
xdate=2017-02-15
xdate=2017-02-16
xdate=2017-02-17
xdate=2017-02-18
xdate=2017-02-19
xdate=2017-02-20
xdate=2017-02-21
xdate=2017-02-22
xdate=2017-02-23
xdate=2017-02-24
xdate=2017-02-23
xdate=2017-02-24
xdate=2017-02-25
xdate=2017-02-26
xdate=2017-02-27
xdate=2017-02-28
xdate=2017-03-01
xdate=2017-03-02
xdate=2017-03-03
xdate=2017-03-04
xdate=2017-03-05
xdate=2017-03-06
xdate=2017-03-07
xdate=2017-03-08
xdate=2017-02-26
xdate=2017-02-28
xdate=2017-03-13
xdate=2017-03-17
xdate=2017-03-25
xdate=2017-03-28
xdate=2017-03-23
xdate=2017-03-24
xdate=2017-03-25
xdate=2017-03-26
xdate=2017-03-27
xdate=2017-03-28
xdate=2017-03-29
xdate=2017-03-30
xdate=2017-03-29
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-05
xdate=2017-04-05
xdate=2017-04-06
xdate=2017-04-06
xdate=2017-04-07
xdate=2017-03-25
xdate=2017-03-26
xdate=2017-03-30
xdate=2017-04-01
xdate=2017-04-02
xdate=2017-04-03
xdate=2017-04-04
xdate=2017-04-08
xdate=2017-04-09
xdate=2017-04-10
xdate=2017-04-11
xdate=2017-04-12
xdate=2017-04-12
xdate=2017-04-13
xdate=2017-04-13
xdate=2017-04-14
xdate=2017-04-15
xdate=2017-04-16
xdate=2017-04-17
xdate=2017-04-18
xdate=2017-04-19
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-19
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-05-05
xdate=2017-05-06
xdate=2017-05-16
xdate=2017-05-19
xdate=2017-05-20
xdate=2017-05-21
xdate=2017-05-22
xdate=2017-05-19
xdate=2017-05-20
xdate=2017-05-21
xdate=2017-05-22
xdate=2017-05-23
xdate=2017-05-24
xdate=2017-05-25
xdate=2017-05-26
xdate=2017-05-22
xdate=2017-05-23
xdate=2017-05-24
xdate=2017-05-25
xdate=2017-05-26
xdate=2017-05-27
xdate=2017-05-28
xdate=2017-05-29
xdate=2017-05-30
xdate=2017-05-31
xdate=2017-06-01
xdate=2017-06-02
xdate=2017-06-03
xdate=2017-06-04
xdate=2017-06-05
xdate=2017-06-06
xdate=2017-06-07
xdate=2017-06-08
xdate=2017-06-09
xdate=2017-06-10
xdate=2017-06-11
xdate=2017-06-12
xdate=2017-06-13
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-30
xdate=2017-07-01
xdate=2017-07-02
xdate=2017-07-03
xdate=2017-07-04
xdate=2017-07-05
xdate=2017-07-06
xdate=2017-07-07
xdate=2017-07-08
xdate=2017-07-09
xdate=2017-07-10
xdate=2017-07-11
xdate=2017-07-12
xdate=2017-07-13
xdate=2017-07-14
xdate=2017-07-15
xdate=2017-07-16
xdate=2017-07-17
xdate=2017-07-18
xdate=2017-07-19
xdate=2017-07-20
xdate=2017-07-21
xdate=2017-07-22
xdate=2017-07-23
xdate=2017-07-24
xdate=2017-07-25
xdate=2017-07-26
xdate=2017-07-27
xdate=2017-07-28
xdate=2017-07-29
xdate=2017-07-30
xdate=2017-07-31
xdate=2017-08-01
xdate=2017-08-02
xdate=2017-08-03
xdate=2017-08-04
xdate=2017-08-05
xdate=2017-08-06
xdate=2017-08-07
xdate=2017-08-08
xdate=2017-08-09
xdate=2017-08-10
xdate=2017-08-11
xdate=2017-08-12
xdate=2017-08-13
xdate=2017-08-14
xdate=2017-08-15
xdate=2017-08-16
xdate=2017-08-17
xdate=2017-08-18
xdate=2017-08-19
xdate=2017-08-20
xdate=2017-08-21
xdate=2017-08-22
xdate=2017-08-23
xdate=2017-08-24
xdate=2017-08-25
xdate=2017-08-26
xdate=2017-08-27
xdate=2017-08-28
xdate=2017-08-29
xdate=2017-08-30
xdate=2017-08-31
xdate=2017-09-01
xdate=2017-05-27
xdate=2017-05-28
xdate=2017-05-29
xdate=2017-05-30
xdate=2017-05-31
xdate=2017-06-01
xdate=2017-06-02
xdate=2017-06-03
xdate=2017-06-04
xdate=2017-06-05
xdate=2017-06-06
xdate=2017-06-07
xdate=2017-06-08
xdate=2017-06-09
xdate=2017-06-10
xdate=2017-06-11
xdate=2017-06-12
xdate=2017-06-13
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-30
xdate=2017-07-01
xdate=2017-07-02
xdate=2017-07-03
xdate=2017-07-04
xdate=2017-07-05
xdate=2017-07-06
xdate=2017-07-07
xdate=2017-07-08
xdate=2017-07-09
xdate=2017-07-10
xdate=2017-07-11
xdate=2017-07-12
xdate=2017-07-13
xdate=2017-07-14
xdate=2017-07-15
xdate=2017-07-16
xdate=2017-07-17
xdate=2017-07-18
xdate=2017-07-19
xdate=2017-07-20
xdate=2017-07-21
xdate=2017-07-22
xdate=2017-07-23
xdate=2017-07-24
xdate=2017-07-25
xdate=2017-07-26
xdate=2017-07-27
xdate=2017-07-28
xdate=2017-07-29
xdate=2017-07-30
xdate=2017-07-31
xdate=2017-08-01
xdate=2017-08-02
xdate=2017-08-03
xdate=2017-08-04
xdate=2017-08-05
xdate=2017-08-06
xdate=2017-08-07
xdate=2017-08-08
xdate=2017-08-09
xdate=2017-08-10
xdate=2017-08-11
xdate=2017-08-12
xdate=2017-08-13
xdate=2017-08-14
xdate=2017-08-15
xdate=2017-08-16
xdate=2017-08-17
xdate=2017-08-18
xdate=2017-08-19
xdate=2017-08-20
xdate=2017-08-21
xdate=2017-08-22
xdate=2017-08-23
xdate=2017-08-24
xdate=2017-08-25
xdate=2017-08-26
xdate=2017-08-27
xdate=2017-08-28
xdate=2017-08-29
xdate=2017-08-30
xdate=2017-08-31
xdate=2017-09-01
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-06-14
xdate=2017-06-15
xdate=2017-06-16
xdate=2017-06-17
xdate=2017-06-18
xdate=2017-06-19
xdate=2017-06-20
xdate=2017-06-21
xdate=2017-06-22
xdate=2017-06-23
xdate=2017-06-24
xdate=2017-06-25
xdate=2017-06-26
xdate=2017-06-27
xdate=2017-06-28
xdate=2017-06-29
xdate=2017-03-27
xdate=2017-04-02
xdate=2017-04-07
xdate=2017-04-08
xdate=2017-04-09
xdate=2017-04-13
xdate=2017-04-14
xdate=2017-04-15
xdate=2017-04-16
xdate=2017-04-17
xdate=2017-04-19
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-20
xdate=2017-04-21
xdate=2017-04-22
xdate=2017-04-23
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-04-29
xdate=2017-04-30
xdate=2017-05-01
xdate=2017-05-02
xdate=2017-04-24
xdate=2017-04-25
xdate=2017-04-26
xdate=2017-04-27
xdate=2017-04-28
xdate=2017-04-29
xdate=2017-04-30
xdate=2017-05-01
xdate=2017-05-02
xdate=2017-05-03
xdate=2017-05-04
xdate=2017-05-05
xdate=2017-05-06
xdate=2017-05-07
xdate=2017-05-08
xdate=2017-05-09
xdate=2017-05-10
xdate=2017-05-11
xdate=2017-05-12
xdate=2017-05-13
xdate=2017-05-14
xdate=2017-05-15
xdate=2017-05-16
ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the
BY expression is missing, zero, or invalid.
SUBJID=106 KEY=106-9 OBS=9 TOTAL=12 STARTDATE=2017-04-25 STOPDATE= CLASS=Steroid / Diuretic
xdate=20934 _ERROR_=1 _N_=52
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 52 observations read from the data set SASUSER.MDM.
WARNING: The data set SASUSER.MDM may be incomplete. When this step was stopped there were 431
observations and 8 variables.
WARNING: Data set SASUSER.MDM was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.38 seconds
cpu time 0.29 seconds```
I don't understand why input doesn't appear to be working. Dates are still listed as character strings under column attributes. The do part also isn't working as intended. I'd be grateful for any further guidance.
Do not use the same name in the DATA and SET statement. Then you're always having to rebuild from the start.
Convert your start and stop date to SAS dates
Remove PUT
Add formats to see them displayed as desired
Drop old variables to avoid confusion.
Your two code steps, the data step and SQL do not appear related. Not sure why you would even need a list of dates for intervals or anything. There are much better ways to calculate an overlap. I think you're putting us through an xy problem where it would be significantly easier to show us what you're attempting to do and people would be able to provide a much better solution.
data sasuser.Mdm2; /*1*/
set sasuser.Mdm;
/*2*/
start_date = input(startdate, yymmdd10.);
end_date = input(stopdate, yymmdd10.);
do xdate = start_date to stop_date;
output; /*3*/
end;
/*4*/
format start_date end_date xDate yymmdd10.;
/*5*/
drop startdate stopdate;
run;
*check;
proc contents data=sasuser.mdm2;
run;
EDIT: Also, if you had some sort of grouping variable to indicate that these were part of the same episode you could then just take the min/max of the dates and subtract them to get the interval duration for starters. Grouping via a data step is trivial.
data want;
set have;
by id;
retain episode;
start_date = input(start_date, yymmdd10.);
end_date = input(stopdate, yymmdd10.);
prev_stop_date = lag(stopDate);
if first.id then do;
episode = 0;
call missing(prev_stop_date);
end;
if not (start_date <=prev_stop_date <= end_date) then episode+1;
*could add in logic to calculate dates and durations as well depending....;
run;
It sounds like your SAS log is complaining about this statement.
do xdate=startdate to stopdate;
Because STARTDATE and STOPDATE are character strings instead of dates.
Make sure to create your date values as dates instead of character strings.
Tom's correct, of course, the startdate and stopdate seem to be characters.
To properly use this, do something like this (only the do loop is relevant for you, the rest is to show it working):
data _null_;
startdate = '2017-03-02';
stopdate = '2017-03-16';
do xdate = input(Startdate,yymmdd10.) to input(stopdate,yymmdd10.);
put xdate= yymmdd10.; *just put to the log to see what you are getting;
end;
run;
input will convert the text to a numeric value. Do realize you have to format that xdate as a date format if you want to be able to view it - if you're just using it as an input, though, you can leave the formatting off.

SAS: When using user defined formats, if there's not a match, "default value" is the unformatted input variable?

In SAS EG, I have a user defined format
value $MDC
'001' = '77'
'002' = '77
...
'762' = '14'
etc.
My data set has DRG_code string variables with values like '001' and '140'.
I was trying to create a new variable, with the below code.
MDC = put(DRG_code, $MDC.)
Only there are more values for the variable DRG_code in my data set, then specified in the user defined format file, $MDC.
For example, when the data set DRG_Code equals '140' this value does not exist in the user defined format, and for some reason the put statement is returning MDC = '14' (which should only be its value with the DRUG code is '762').
Is there a way to make sure my put statement only returns a value from the user defined format when a corresponding value is present?
Grateful for feedback.
Lori
I've tried using formatting like "length" to have my put statement return 3, which I thought would result in "140" instead of "14" and that didn't work.
value $MDC
'001' = '77'
'002' = '77
...
'762' = '14'
MDC = put(DRG_code, $MDC.)
Formats have a DEFAULT width. If you do not specify a width when using the format then SAS will use the default width. When making a user defined format PROC FORMAT will set the default width to the maximum width of the formatted values. In your example the default width is being set to 2.
You can override that when you use the format.
MDC = put(DRG_code, $MDC3.)
Or you could define the default when you define the format.
value $MDC (default=3)
'001' = '77'
'002' = '77'
...
'762' = '14'
;
You can also set a default value for the unmatched values using the other keyword.
value $MDC (default=3)
'001' = '77'
'002' = '77'
...
'762' = '14'
other = 'UNK'
;
You can even nest a call to another format for the unmatched values (or any target format). In which case you do not need to specify the default width since the width on the nested format will be used when defining the default width.
value $MDC
'001' = '77'
'002' = '77'
...
'762' = '14'
other = [$3.]
;
I presume all the value mappings were $2 because that is what is used for an 'unfound' source value. In order to ensure the length of 'unfound' values, make sure one of the formatted values has trailing spaces filling out to length of longest unfound value.
value $MDC
'001' = '77 ' /* 7 characters, presuming no DRG_code exceeds 7 characters */
'002' = '77'
'762 = '14'
You can also fix this by specifying a length to use when applying the format, e.g.
proc format;
value $MDC
'001' = '77'
'762' = '14'
;
run;
data _null_;
do var = '001','140','762';
var_formatted = quote(put(var,$MDC3.));
put var= var_formatted=;
end;
run;
Output:
var=001 var_formatted="77 "
var=140 var_formatted="140"
var=762 var_formatted="14 "
N.B. both this solution and Richard's will result in trailing whitespace being added to formatted values, as you can see from the quotes.
Here I propose a slight modification to user667489's solution so that:
you don't need to specify the length of the format every time you use it (using the default option of the value statement when defining the format)
the resulting formatted value doesn't have trailing blanks (using the trim() function on the output resulting from applying the format)
i.e.
proc format;
value $MDC(default=3)
'001' = '77'
'002' = '77'
'762' = '14'
;
run;
data _null_;
do var = '001', '140', '762';
var_formatted = quote(trim(put(var, $MDC.)));
put var= var_formatted=;
end;
run;
which gives the following output:
var=001 var_formatted="77"
var=140 var_formatted="140"
var=762 var_formatted="14"

Using If-then statements in SAS, multiple variables

I am using the SAS-University edition.
I need to create a new ethnicity variable based on existing sub ethnicity.
Is there a way to select 'WHITE all' instead of specifying 'WHITE-RUSSIAN' or 'WHITE-EUROPEAN' and create the new variable.
Here is my code.
data agg1;
set agg;
if ethnicity = 'WHITE' then ethnicity1= 'white' ;
if ethnicity = 'WHITE-RUSSIAN' then ethnicity1= 'white' ;
if ethnicity = 'WHITE-EUROPEAN' then ethnicity1= 'white';
.
.
run ;
Use a : modifier:
if ethnicity eq: 'WHITE' then ethnicity1= 'white' ;
Assuming the values of the ethnicity variable are always in the format '<main>-<sub>', you can reduce this to one line and zero if statements:
data agg1;
set add;
ethnicity1=scan(ethnicity,1,'-');
run;

Convert date format to character string

I have a column of format DATETIME23. like this:
14.02.2017 13:00:25
I want to conver it to a string, so later, i would be able to modern it, so, for example, the final version would look like:
2017-02-14 13:00:25.000
Problem occures, when i try to convert date to char format: in result i have a string of smth like 1802700293 - which is the number of seconds.
I tried:
format date $23.0
or
date = put(date, $23.0)
P.S This is nother try:
data a;
format d date9.;
d = '12jan2016'd;
dtms = cat(day(d),'-',month(d),'-',year(d),' 00:00:00.000');
/* если нужно обязательно двухзначные день и месяц, то такой колхоз: */
if day(d) < 10 then dd=cat('0',put(day(d),$1.));
else ddday=put(day(d),$2.);
if month(d) < 10 then mm=cat('0',put(month(d),$1.));
else mm=put(month(d),$2.);
yyyy=put(year(d),$4.);
/*dtms2 = cat(dd,'-',mm,'-',yyyy,' 00:00:00.000');*/
dtms2 = cat(dd,'-',mm,'-',yyyy,' 00:00:00.000');
dtms = cat(day(d),'-',month(d),'-',year(d),' 00:00:00.000');
run;
BUT, abnormally, the dtms2 concat destroys the zero in the month element
If your datetime is stored as a SAS datetime, just use the appropriate format :
data test ;
dt = '09feb2017:13:53:26'dt ; /* specify a datetime constant */
new_dt = put(dt,E8601DT23.3) ; /* ISO datetime format */
run ;
Output
dt new_dt
1802267606 2017-02-09T13:53:26.000
If you need to replace the 'T' with a space, simply add a translate function around the put().
For your dtms solution you can use put and the Z2. format to keep the leading zero when you concatenate:
dtms = cat(day(d),'-', put(month(d),z2.),'-',year(d),' 00:00:00.000');
You should be able to just use put(date, datetime23.) for your problem though instead of $23, which is converting the number of seconds to a string with length 23. However, as a comment has mentioned datetime23. is not the format from your example.