1) Converting character date values into numeric:
/*I) Using the DATASTEP:*/
1)
Data dates; input cdate $9.; cards;
16-apr-07
01-01-07
02-jun-07
13-sep-07
; run;
Data Convert; set dates;
Date = input( cdate , ANYDTDTE9.); format date date7.;
run;
2)
Data dates;
input cdate $9.; cards;
16-apr-2007
01-01-2007
02-jun-2007
13-sep-2007
; run;
Data Convert;
set dates;
Date = input( cdate , ANYDTDTE11.);
format date date9.; run;
*II) Using Proc SQL;
*Numeric date variable can be converted to character date variable by using the PUT function within PROC SQL.;
proc sql;
create table date_char as
select PUT(date,date9.)as ndate
from date_num; quit;
*Character date variable can be converted to numeric date variable by using the INPUT function within PROC SQL.;
Proc sql;
create table date_num as
select INPUT (date,mmddyy10.)as ndate
format=mmddyy10.
from date_char; quit;
Or
Proc sql;
create table date_num as
select INPUT (date,date9.)as ndate format=date9.
from date_char; quit;
III) Using Arrays;
*using arrays to convert character date variables aestdtc and aeendtc into numeric variables:
data c_date;
set date;
array cha_date { 2 } $ 10 aeendtc aestdtc; array num_date { 2 } aeendt aestdt;
do i = 1 to 2;
num_date{ I } = input(cha_date{ i }, anydtdte10.);
end;
drop aeendtc aestdtc; run;
*Using Arrays converting variables from numeric to character;
data new; set old (rename=(b1=a1 b2=a2 b3=a3 b4=a4 b5=a5));
array aa $ b1 b2 b3 b4 b5;
array bb a1 a2 a3 a4 a5; do I=1 to dim(aa);
aa (i)= put(bb(i),8.0);
end;
drop I a1 a2 a3 a4 a5 a6; run;
*Using Arrays converting variables from character to numeric;
*By using the INPUT instead of PUT we can convert character variables into numeric;
data new ;
set old (rename=(b1=a1 b2=a2 b3=a3 b4=a4 b5=a5));
array aa b1 b2 b3 b4 b5;
array bb $ a1 a2 a3 a4 a5;
do I=1 to dim (aa);
aa (i)=input(bb(i),8.0);
end;
drop I a1 a2 a3 a4 a5 a6;
run;
Note: Dont forget to use $ sign inside the array statement when converting the vars from Num to char or Char to num.
Welcome to StudySAS, your ultimate guide to clinical data management using SAS. We cover essential topics like SDTM, CDISC standards, and Define.XML, alongside advanced PROC SQL and SAS Macros techniques. Whether you're enhancing your programming efficiency or ensuring compliance with industry standards, StudySAS offers practical tips and insights to elevate your clinical research expertise. Join us and stay ahead in the evolving world of clinical data.
Discover More Tips and Techniques on This Blog
Subscribe to:
Post Comments (Atom)
Disclosure:
In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers.
My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.
Bravo The proc Sql to convert numeric date to character is really efficient
ReplyDelete