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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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);

View solution in original post

3 REPLIES 3
Loko
Barite | Level 11

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.

Reeza
Super User

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);

myname
Calcite | Level 5

works exactly how I wanted it to. I did not know the vtype function, thank you!

sas-innovate-white.png

Register Today!

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!

Register now!

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
  • 3 replies
  • 1282 views
  • 1 like
  • 3 in conversation