DATA Step, Macro, Functions and more

Changing formats based on the format?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Changing formats based on the format?

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!


Accepted Solutions
Solution
‎07-04-2016 10:10 AM
Super User
Posts: 19,878

Re: Changing formats based on the format?

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


All Replies
Super Contributor
Posts: 308

Re: Changing formats based on the format?

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.

Solution
‎07-04-2016 10:10 AM
Super User
Posts: 19,878

Re: Changing formats based on the format?

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

Occasional Contributor
Posts: 10

Re: Changing formats based on the format?

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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