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

I have a large dataset with several hundred variables that have a YYMMDD10. informat. Is there an easy way to mass apply a MMDDYY10. format to all of these variables without individually listing them each out in a format statement? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

NO.  But that doesn't mean that YOU have to list them yourself.  Let SAS do it for you.

First let's make a couple of macro variables so we don't have to keep re-typing the name of the dataset. Then we can ask SAS to create another macro variable with the variables we want to change. Then use PROC DATASETS to change the formats.

%let lib=WORK;
%let ds=HAVE;
proc contents data=&lib..&ds noprint out=contents; run;
proc sql noprint;
select nliteral(name) into :datevars separated by ' '
  from contents
  where format='YYMMDD'
;
quit;
proc datasets nolist lib=&lib;
  modify &ds ;
    format &datevars mmddyy10.;
  run;
quit;

PS Why do you want to convert from a date style that everyone will understand to one that will confuse 50% of the world?  Does 10/12/2021 mean October twelfth or the tenth of December? 

View solution in original post

6 REPLIES 6
AMSAS
SAS Super FREQ

It will come down to how your variables are named, for example in the example below I have assigned DDMMYY10. to date2 without having to specify date2.

 

data have ;
	do date1="23NOV2021"d to "28NOV021"d ;
		date2=date1+7 ;
		date3=date2+7 ;
		output ;
	end ;
run ;

data want ;
	set have ;
	format date1-date3 ddmmyy10. ;
run ;

Now if your date variables have non-sequential variable names e.g. dateOne, dateTwo, dateThree then that's going to be more challenging.

First question would be how do you know any of these variables are date variables and need to be formatted DDMMYY10.

 

Tom
Super User Tom
Super User

NO.  But that doesn't mean that YOU have to list them yourself.  Let SAS do it for you.

First let's make a couple of macro variables so we don't have to keep re-typing the name of the dataset. Then we can ask SAS to create another macro variable with the variables we want to change. Then use PROC DATASETS to change the formats.

%let lib=WORK;
%let ds=HAVE;
proc contents data=&lib..&ds noprint out=contents; run;
proc sql noprint;
select nliteral(name) into :datevars separated by ' '
  from contents
  where format='YYMMDD'
;
quit;
proc datasets nolist lib=&lib;
  modify &ds ;
    format &datevars mmddyy10.;
  run;
quit;

PS Why do you want to convert from a date style that everyone will understand to one that will confuse 50% of the world?  Does 10/12/2021 mean October twelfth or the tenth of December? 

mmm7
Calcite | Level 5

Thanks @Tom ! Your code worked great and will save me SO much time! 

mmm7
Calcite | Level 5

@Tom Do you have syntax to clear all formatting EXCEPT for formatted date and time variables? 

Tom
Super User Tom
Super User

Use the same code, just add an extra FORMAT statement in the last step.

proc datasets nolist lib=&lib;
  modify &ds ;
    format _all_ ;
    format &datevars mmddyy10.;
  run;
quit;
Tom
Super User Tom
Super User

To find DATE, TIME or DATETIME variables to exclude or re-attach formats to use the FMTINFO() function with the 'CAT' info type.

proc sql noprint;
select nliteral(name) into :othervars separated by ' '
  from contents
  where not (fmtinfo(format,'cat') in ('date','time','datetime'))
;
proc datasets nolist lib=&lib;
  modify &ds ;
    format &othervars;
  run;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 865 views
  • 3 likes
  • 3 in conversation