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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 929 views
  • 1 like
  • 3 in conversation