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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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.;

 

 

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
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.;

 

 

 

--
Paige Miller
Tom
Super User Tom
Super User

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.

Traian
Fluorite | Level 6

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 9372 views
  • 2 likes
  • 3 in conversation