BookmarkSubscribeRSS Feed
sassy_seb
Quartz | Level 8

I wanted to know what would be the most efficient way to loop through each variable in my SAS code and return the data type.

 

I was also thinking of changing the format of variables depending on the data type. 

For example, if var1 is Num, set format to a specific one for numerical variables, and so forth for character variables.

 

Is there a way for me to loop through and efficiently change all of the variables formats using an IF/ELSE statement?

7 REPLIES 7
Astounding
PROC Star

If all you want to do is use one format for numerics and a different format for character variables, you don't need to do so much work.  You could use:

 

data want;
   set have;
   format _numeric_ numformat.  _character_ $charformat.;
run;

Of course you have to supply the format names.  Perhaps there is more to the problem that I am failing to see.

sassy_seb
Quartz | Level 8

I was interested in the loop since I would have to change it for a lot of variables. It's more than a thousand. So rather than listing each variable and setting the specific format, I thought a loop would be helpful in automating the process.

 

PaigeMiller
Diamond | Level 26

The approach from @Astounding above not only meets your stated needs, but does not require listing a thousand variable names and does not require a loop of any sort.

 

When you see _numeric_ in SAS code, that is the exact same thing as typing all the names of the numeric variables. When you see _character_ in SAS code, that is the same as typing the names of all the character variables.

--
Paige Miller
sassy_seb
Quartz | Level 8

I'm sorry, I didn't understand what _numeric_ and _character_ did at first but now I do! I will try this out, thank you!

sassy_seb
Quartz | Level 8

Another question, is there a way to do this with numeric variables storing dates? 

PaigeMiller
Diamond | Level 26

Do you mean ... can you assign different formats to dates than to other numeric variables? In that case, you would need to somehow identify which variables are dates and which variables are numeric but not dates. Maybe a loop, or maybe some other method but now we are into a lot more typing and a lot more programming.

--
Paige Miller
Tom
Super User Tom
Super User

No LOOPING is required.  Just get the DATA and act on the DATA.

In this case the data you want to act on is the metadata about the dataset.  You can get that from PROC CONTENTS. You can also get it from DICTIONARY.COLUMNS (note you can also access DICTIONARY.COLUMNS via the view SASHELP.VCOLUMN but the performance could be worse if you have a lot of libraries defined) but the way PROC CONTENTS stores the FORMAT information is more useful for this problem.

 

So something like this could be use to make a variable FMTSPEC with the format you want attached to the variable.  Normal character strings and numbers do not need any format attached to them, but DATE, TIME and DATETIME values do.

proc contents data=have noprint out=contents; run;
data new_formats;
  set contents;
   length fmtspec $40;
   if type=2 then fmtspec=' ';
   else select (fmtinfo(format,'cat') ;
     when ('date') fmtspec='yymmdd10.';
     when ('time') fmtspec='tod8.';
     when ('datetime') fmtspec='datetime19.';
     otherwise fmtspec=' ';
  end;
run;

Since changing the format attached to a variable requires CODE you will have to use some method of code generation.  Assuming your set of variables is normal sized you could use macro variables.  But if the list is long you might want to generate a file with the code instead.

filename code temp;
data _null_;
   file code ;
   set new_formats ;
   by fmtspec notsorted;
   length nliteral $64 ;
   nliteral=nliteral(name);
   if  first.fmtspec then put 'format ' @;
   put nliteral @;
   if last.fmtspec then put fmtspec ';' ;
run;

Now that you have a file with the format statements in it you can use that code to attach the formats.

You could do it as part of a data step to make a new dataset:

data want;
  set have;
%include code / source2;
run;

Or you could do it as part of PROC DATASETS step to modify the formats attached to the existing dataset.

proc datasets nolist lib=WORK;
  modify have;
%include code / source2;
  run;
quit;

 

 

sas-innovate-wordmark-2025-midnight.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. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1131 views
  • 8 likes
  • 4 in conversation