Hi,
I wonder if someone can offer a solution to my problem of assigning SAS formats to a list of variables.
I So, have a list of variables with a variety of SAS variable naming conventions (e.g., SEX, AGE_GROUP, var1-var100, a1A, a1B, etc.). I also have formats for each variable using the name of the variable plus an 'f' suffix as a naming convention (e.g., SEXf, AGE_GROUPf, var1f, a1Af, etc).
Instead of writing a format statement that assigns the format for each variable (e.g., format var1 var1f.;) I wonder if it is possible to do this with a %array and a macro statement.
Thanks in advance.
--Tim
Pull the list of variables from metadata. Assume you have a dataset named WORK.HAVE.
This will work if all of your variables are numeric.
proc sql noprint ;
select catx(' ',name,catx(name,'F.'))
into :varlist separated by ' '
from dictionary.columns
where libname = 'WORK'
and mename = 'HAVE'
;
quit;
Now you can use &VARLIST in a FORMAT statement of another DATA or PROC step. Or you could use PROC DATASETS to modify the data set and permanently attach the formats.
proc print data=have;
format &varlist ;
run;
If some of your variables are character then you should really use "$nameF." for the format name, but SAS usually will automatically correct that type of error. That is if you try to attach character format $SEXF. to a numeric variable SAS will automatically instead search for the numeric format SEXF.
Pull the list of variables from metadata. Assume you have a dataset named WORK.HAVE.
This will work if all of your variables are numeric.
proc sql noprint ;
select catx(' ',name,catx(name,'F.'))
into :varlist separated by ' '
from dictionary.columns
where libname = 'WORK'
and mename = 'HAVE'
;
quit;
Now you can use &VARLIST in a FORMAT statement of another DATA or PROC step. Or you could use PROC DATASETS to modify the data set and permanently attach the formats.
proc print data=have;
format &varlist ;
run;
If some of your variables are character then you should really use "$nameF." for the format name, but SAS usually will automatically correct that type of error. That is if you try to attach character format $SEXF. to a numeric variable SAS will automatically instead search for the numeric format SEXF.
Thanks for the help Tom.
My database doesn't include CHARACTER fields, but it's helpful to know how to assign a character format using this approach.
--Tim
example(red parts must in upcase😞
proc sql noprint;
select catx(' ',name,cats(name,'f.')) into :names separated by ' '
from dictionary.columns
where libname='SASHELP' and memname='CLASS';
quit;
%put &names;
This article may be useful :
Tim,
The posted solutions are pointing in the right direction ... there's another issue I wanted to raise. Does &VARLIST include all the variables in your data set, or just some. At this point, the solutions generate formats for every variable in the data set. If &VARLIST contains just a subset, you may want to start off with:
proc contents data=have (keep=&varlist) noprint out=_contents_ (keep=name);
run;
Then you could use _contents_ instead of dictionary.columns in the proposed solutions.
Also note that you could save TYPE as well as NAME in the output data set. That might be helpful if you want to add $ at the beginning of the format names for character variables.
Thanks Astounding,
For this problem, the &VARLIST would only include variables that require a format. Therefore, not all of the variables in my database would be included in the list. So, the _contents_ solution would be helpful.
Thanks.
--Tim
If you already have the list of variables then simple string manipulation will let you generate the required list of name/format pairs.
%let varlist=age sex;
data _null_;
length varlist fmtlist $3000 ;
varlist=symget('varlist');
do i=1 to countw(varlist);
fmtlist=catx(' ',fmtlist,scan(varlist,i),scan(varlist,i)||'F.');
end;
call symputx('fmtlist',fmtlist);
run;
....
format &fmtlist ;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.