I have around 26 variables ranging from ia1_1, ia1_2, ia_3 ... ia_26. I would like to apply a format to them.
data test_2; set test_1; Format ia1_1 format_test.; run;
If i do this, i can apply it to one variable. I was wondering if there was a quick way to somehow apply it to all of these instead of manually doing it for each one. Note: these are not the only variables in the table. I have little experience in SAS and not knowing how to optimize simple things drives me crazy.
format ia1_1 ia1_2 ia_3 ... ia_26 format_test.;
Where the ... (triple dots) indicate you have to type in the variable names you want to have this format.
Please note that your variables don't form a pattern of variable names with consecutive numbers, if it did, you could do something like this:
format ia_1-ia_26 format_test.;
format ia1_1 ia1_2 ia_3 ... ia_26 format_test.;
Where the ... (triple dots) indicate you have to type in the variable names you want to have this format.
Please note that your variables don't form a pattern of variable names with consecutive numbers, if it did, you could do something like this:
format ia_1-ia_26 format_test.;
You can list as many variables in a single FORMAT statement as you want. If you want to apply the same format to multiple variables you only need to list format specification once, right after the list of names. You can apply different formats to different variables in the same FORMAT statement by inlcluding as many variable_list format_specification pairs as you need. Note if you want to REMOVE any attached format then list the variables without any format specification. That one obviously has to be the list list/format combination in the FORMAT statement.
You can use variable lists to reduce the amount of typing. If you use sequential numeric suffixes. var1-var23
Or if you know their order in the "data vector" (their order in the dataset being written) you can use positional variable list. firstvar -- lastvar. You also specify numeric, character or all variables by using _numeric_, _character_ or _all_. And you can limit positional lists to one type: firstvar-numeric-lastvar.
And more recently they added name prefix lists. So all variables that start with a constant set of characters. Like in your case you might be able to use ia: to include all of those variables.
You can include more than one statement in a data step. So even if you did not want to make one long FORMAT statement you could just include many shorter ones. Or other things you want to do in that step, like perform calculations or rename variables or attach labels to variables, etc.
At first i was thinking about making a variable list as you said. I realized however after Mr PaigeMiller's answer that i can indeed use the prefix list feature that you also mentioned.
So all i had to do was
format ia1_1-ia1_26 format_test.;
Thank you for taking the time to clarify these methods though.
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.