Hello everyone,
I have several data sets that all contain the same variables. Problem is, one date variable is sometimes stored as a date variable, sometimes as a character variable. There is not apparend systematic to which is which and I dont want to check every data set (there are about 50).
So far I have this code:
it works well, when the variable (published) is a character variable, but when it is a datetime variable it just gives me missing.
%macro published(have);
%do i=1 %to 50;
DATA work.want
set &have._&i.;
published_1 = input(published ,ANYDTDTE16.);
format published_1 date9.;
RUN;
%end;
%mend published;
%published(have)
Any Idea how I could run this for all data sets?
(Btw: when published is stored as a date variables it is a datetime variable, and I only need the date.)
Thanks a lot!
Use conditional logic, untested. Not sure what VType returns or if this might throw an error due to mixing of types.
If vtype(published)='C' then published_i=input...;
else published_i=datepart(published);
Hello,
There is no such thing as datetime variable in SAS. There are only 2 types of variables in SAS: numeric and character.
I suggest you check the type of variable stored within each dataset (using the dictionaries - sashelp.vcolumn, in this case)
and act accordingly based on type of variable.
Use conditional logic, untested. Not sure what VType returns or if this might throw an error due to mixing of types.
If vtype(published)='C' then published_i=input...;
else published_i=datepart(published);
works exactly how I wanted it to. I did not know the vtype function, thank you!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Save $200 when you sign up by March 14!
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.