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

Hello,

 

I am using the output delivery system in proc surveyfreq to output a data set that contains my desired statistics: RowPercent, RowLowerCL, RowUpperCL. The following code works to give me some of what I want, but not everything and not in the desired layout.

 

proc surveyfreq data=data1;
cluster school;
strata class;
weight stwt;
table (gender gradeid youth [other vars]l)*(cursmk eversmk [other vars])/ cl row nocellpct nostd nofreq nowt nototal;
ods output CrossTabs=ResponseTable;
run;

I would appreciate any advice on the following questions: 

 

-Is there a way to suppress the "F_[var]" columns from appearing?

-Is there a way to get the variable columns to list, for example, "yes" and "no" instead of 0 and 1?

-Is there any way to shift the position of the columns so that the demographic variables can be listed together followed by the two response variable columns in the output data set (e.g., having the gender and gradeid column next to each other and the cursmk and eversmk columns next to each other) without having to manually perform this task in Excel?

 

 I have attached two screenshots that show the resulting data set I get when I run the above SAS code. Thank you for your help.

 photo 1.jpgphoto 2 jpeg.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@jag07g wrote:

Thank you. I was thinking proc format would work for this, but it is not outputting my value labels when I run surveyfreq.  Here is my code: Does proc format need to be listed in a different location?

libname fyts17 'C:\User; 
data dataset_new;
set dataset_old; 
format _all_;

proc format;
value sex
0="Male"
1="Female"
;
run;

proc surveyfreq data=dataset_new;
weight stwt;
table (gender youth schlvl)*(eversmk cursmk)/ cl row nocellpct nostd nofreq nowt nototal;
format gender sex.;
ods output CrossTabs=ResponseTable;
run;

Format names have a dot at the end to indicate that they are formats and not variables, see the red dot above. For formats that display decimal values a digit after the dot indicates the number of decimals.

You can also change the number of displayed characters by providing a digit: format gender sex1. ; would only display the M or F value. Which is why your custom format names cannot end in a digit.

View solution in original post

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

Just add a datastep after your code to do the additional formatting, maybe:

data responsetable;
retain /* put your column order required here */ set responsetable (drop=f_:);
/* Decode data here like
...=ifc(strip(...)="1","Male","Female");
with the ... replaced by variable names */
run;
jag07g
Fluorite | Level 6

Thank you--this solved my problem of column order and removing the formatted variable columns. I am still a little unsure about the code to change 0,1 into labels.

 

For gender, I would like to list 0 for "male" and 1 for "female" and in other cases, like for cursmk, I would like 1 to appear as "yes" and 2 to appear as "no." Would the code look like this? Does any command come before listing the variable name. I am sorry I am not familiar with ifc and strip options. Thank you very much again.

.....
set ResponseTable4 (drop=F_gender F_eversmk F_gradeid F_ethnicityid F_sportsid F_swatid F_youth F_schlvl F_cursmk); gender=ifc(strip(gender))="0","Male","1","Female"); cursmk=ifc(strip(cursmk))="1", "Yes", "2", "No");
[other vars].......
ballardw
Super User

@jag07g wrote:

Thank you--this solved my problem of column order and removing the formatted variable columns. I am still a little unsure about the code to change 0,1 into labels.

 

For gender, I would like to list 0 for "male" and 1 for "female" and in other cases, like for cursmk, I would like 1 to appear as "yes" and 2 to appear as "no." Would the code look like this? Does any command come before listing the variable name. I am sorry I am not familiar with ifc and strip options. Thank you very much again.

.....
set ResponseTable4 (drop=F_gender F_eversmk F_gradeid F_ethnicityid F_sportsid F_swatid F_youth F_schlvl F_cursmk); gender=ifc(strip(gender))="0","Male","1","Female"); cursmk=ifc(strip(cursmk))="1", "Yes", "2", "No");
[other vars].......

Formats are your friend.

Proc format;

value sex

0='Male'

1='Female'

;

run;

It appears that your gender variable is actually numeric.

Then when you want to see the Male/Female instead of 0/1 use the format. If you add:

format gender sex. ;

in the surveyfreq code the format will be assigned to the output and the default appearance as long as SAS can find your format (is active in the current session) it will display Male/Female by default.

The format could be permanently assigned to your output set with proc datasets or in any report or further analysis step.

The Yes/no format would be very similar.

jag07g
Fluorite | Level 6

Thank you. I was thinking proc format would work for this, but it is not outputting my value labels when I run surveyfreq.  Here is my code: Does proc format need to be listed in a different location?

libname fyts17 'C:\User; 
data dataset_new;
set dataset_old; 
format _all_;

proc format;
value sex
0="Male"
1="Female"
;
run;

proc surveyfreq data=dataset_new;
weight stwt;
table (gender youth schlvl)*(eversmk cursmk)/ cl row nocellpct nostd nofreq nowt nototal;
format gender sex;
ods output CrossTabs=ResponseTable;
run;
ballardw
Super User

@jag07g wrote:

Thank you. I was thinking proc format would work for this, but it is not outputting my value labels when I run surveyfreq.  Here is my code: Does proc format need to be listed in a different location?

libname fyts17 'C:\User; 
data dataset_new;
set dataset_old; 
format _all_;

proc format;
value sex
0="Male"
1="Female"
;
run;

proc surveyfreq data=dataset_new;
weight stwt;
table (gender youth schlvl)*(eversmk cursmk)/ cl row nocellpct nostd nofreq nowt nototal;
format gender sex.;
ods output CrossTabs=ResponseTable;
run;

Format names have a dot at the end to indicate that they are formats and not variables, see the red dot above. For formats that display decimal values a digit after the dot indicates the number of decimals.

You can also change the number of displayed characters by providing a digit: format gender sex1. ; would only display the M or F value. Which is why your custom format names cannot end in a digit.

jag07g
Fluorite | Level 6
Thanks so much! I was forgetting the dot.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 927 views
  • 0 likes
  • 3 in conversation