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

Hello,

 

I am trying to modify the formatting for variables in SAS 9.4 using 'proc format'. An example is the variable "sCYSleep" which ranges from 0-16. I am trying to create groups (as in the picture) with these values. When running frequency tables right after assigning formatting, I get the respective groups. However, whenever I run subsequent analysis, my sCYSleep variable takes on it's original 0-16. 

 

Is there a way it can retain the formatted groupings? I'd like to have those in the dataset. I tried to manually change the values and SAS scrambles the order. Is there a way to also preserve the natural ordering of the variable names? 

Screenshot 2023-06-27 at 2.49.40 PM.png

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Reeza_0-1687894454394.png

 

That should work for you.

View solution in original post

10 REPLIES 10
awesome_opossum
Obsidian | Level 7

I think it would work if you apply the format in a data step instead of inside proc freq.  The format inside proc freq, I think, would only be held within that running of the procedure.  Doing it in a data step will apply the format "permanently" (i.e. in subsequent analytic steps, like proc freq). 

 

data work.val; set work.val; 
format sleepvar fmt.; 
run;

 

sbxkoenk
SAS Super FREQ

Hej,

 

Permanent Informats and Formats

If you want to use a format or informat that is created in one SAS job or session in a subsequent job or session, then you must permanently store the format or informat in a SAS catalog.

You permanently store informats and formats by using the LIBRARY= option in the PROC FORMAT statement.

See the discussion of the LIBRARY= option in the PROC FORMAT Statement.

 

Also in doc :

Accessing Permanent Informats and Formats


Concepts: FORMAT Procedure

https://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#p0owakbeh7u19cn0zrqf...

 

Koen

Reeza
Super User

Reeza_0-1687894454394.png

 

That should work for you.

ValSki
Calcite | Level 5

Reeza,

 

Thank you for this solution!

 

Do you know how to tell SAS to keep the categories in order (i.e., none, low, etc.). When I run frequency tables pr any subsequent stats, the categories are all jumbled up. It appears to be listed in alphabetical order (i.e., high, low, etc.).

Reeza
Super User
If you use the original variable with the format applied it will sort correctly. If you use the converted variable, a character variable, it sorts alphabetically.
ValSki
Calcite | Level 5
Awesome, thank you!
awesome_opossum
Obsidian | Level 7

Reeze are you studying sleep probs again?   You're going to make a helpful accident, which you do every time!

Tom
Super User Tom
Super User

"Natural ordering of the variable names"?

 

What does that mean?

 

If you make a new variable it will naturally be added after all of the existing variables.

 

If you convert a number to a character string it will naturally sort differently.  If you want the text values to sort in the same order as the original numeric values then you will need to be more creative in the strings you using.  Perhaps add a prefix to the strings?

ValSki
Calcite | Level 5
Hi Tom,

I meant to preserve the hierarchical nature of the original variable names (i.e., none; low; moderate, etc.).

I can preserve the ordering if I use the original variable name with the formatting applied. That seemed to have solve the problem of alphabetical ordering.
Tom
Super User Tom
Super User

@ValSki wrote:
Hi Tom,

I meant to preserve the hierarchical nature of the original variable names (i.e., none; low; moderate, etc.).

I can preserve the ordering if I use the original variable name with the formatting applied. That seemed to have solve the problem of alphabetical ordering.

So your did not mean variable names.  Your variable name is SCYSLEEP.

 

You are talking about VALUES not NAMES.  In particular the displayed values.  So either the formatted values of your existing numeric variable or the values stored into your new character variable.

 

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!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 10 replies
  • 1739 views
  • 4 likes
  • 5 in conversation