BookmarkSubscribeRSS Feed
LOLO
Obsidian | Level 7

I have a data set that several variables that are numeric and have value label formats that are  text. I am doing a proc freq with an output option and would like to stack these variables on top of each other while maintaining the 'text' value label and NOT the numeric value. Everytime I do this every output after the first variable will take on the value formats of the first variable. Is there a way to convert the value labels into the value?? 

 

Thank you!!!

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, your post is confusing.  Do you mean that when you set the datasets together the variable is set to the label of the first dataset - this is normal, as a variable can only have one name, and one label.  Post example test data - in the form of a datastep - and what the output should look like, maybe if you want subheadings:

data want (keep=category);
  length category $20;
  set freq_out1 (in=a) freq_out2 (in=b);
  if a then do;
    category="Freq heading1";
    output;
    category=cats('  ',put(n,best.));
    output;
  end;
  if b then do;
...
run;

This will output a row for the title of the variable, then the result (indented a bit).

LOLO
Obsidian | Level 7

hi! Thanks-- no I was actually talking about the value labels. Say I have two questions;

gender:

1=male

2=female

 

and then another question

ethnicity:

1= hispanic

2=black

3= asian...

 

 

I want my freq output file to look like:

value   count

female  4

male     6

black   2

hispanic 7

asian     1

 

 

but what I end up getting is something like:

value  count

female   4

male      6

female   2

male      7

 

 

because it is taking the format of the gender variable and applying it to ethnicity. so My thought was to somehow turn those value labels into the actual text values. Any ideas?????

 

Thank you!

 

ballardw
Super User

Please show the code you are using.

You symptom does look possible a FORMAT statement has a problem, possibly Format gender race Genderformat. RaceFormat;

which should be

Format gender genderformat. race raceformat. ;

LOLO
Obsidian | Level 7

proc freq data =dsn ;

tables gender\output out=dsn1 ;

data dsn1 ; set dsn1 ;

rename gender=value ;

 

proc freq data = dsn ;

tables ethnicity\output out=dsn2 ;

data dsn2 ; set dsn2 ;

rename ethnicity=value ;

 

 

data both ; set dsn1 dsn2 ;

 

run ;

 

I think the big question is can you change the value label into the value, so instead of the value being 1 can I make it be the label, e.g. female, male....

 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, the answer is per my post above.  I have updated your code to read easily and commented below,

 

 

proc freq data=dsn;
  tables gender \ output out=dsn1;
run;

data dsn1; 
  set dsn1;
/* At this point, your dsn1 contains a variable called value, which has the attributes of gender */
  rename gender=value;
run;
 
proc freq data=dsn;
  tables ethnicity \ output out=dsn2;
run;

data dsn2;
  set dsn2;
/* At this point, your dsn1 contains a variable called value, which has the attributes of ethnicity */
  rename ethnicity=value;
run;
 
data both; 
/* At this point you have two variables which are both numeric, but have different attributes, one with gender, one with ethinicity.  As the gender one is first in the set statement its attributes are carried forward, hence value takes the format of gender */
  set dsn1 dsn2;
run ;

Basically a column can only have one set of properties - length, label, format, etc.  Now as gender (value) is first in the set statement the format for gender is applied to the whole variable including the ethnicity values.  The easiest way round this is to convert both to character and put them into the respective formats:

 

proc freq data=dsn;
  tables gender \ output out=dsn1;
run;

data dsn1;
length value $200; set dsn1; value=put(gender,gender.); run; proc freq data=dsn; tables ethnicity \ output out=dsn2; run; data dsn2;
length value $200; set dsn2; value=put(ethnicity,ethinicty.); run; data both; set dsn1 dsn2; run ;

 You should now see a character field value which has the relevant values.

ballardw
Super User

Perhaps starting more from scratch.

Consider what this result looks like:

 

proc tabulate data=dsn;
   class gender ethnicity;
   table  gender ethnicity,
          n colpctn ;
   format  gender gender. ethnicity ethnicity.;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2369 views
  • 0 likes
  • 3 in conversation