BookmarkSubscribeRSS Feed
Wolverine
Quartz | Level 8

I'm trying to format a frequencies output dataset. The data looks like this:

varname COUNT PERCENT

 

20 .200000000
0 50 .500000000
1 30 .300000000

 

I want it to look like this:

_NAME_ 0 PERCENT_0 1 PERCENT_1 missing PERCENT_m
varname 50 .50 30 .30 20 .20

 

Current code:

PROC TRANSPOSE data=have out=want;
	VAR varname;
	BY COUNT PERCENT; 
RUN;
6 REPLIES 6
PaigeMiller
Diamond | Level 26

Do you want this wide arrangement of the data to produce a report? If so, leave the data un-transposed and use PROC REPORT which will give you a wide report on the un-transposed data.

--
Paige Miller
Wolverine
Quartz | Level 8

Yes, I'm trying to create a table with about 10 variables. My plan was to transpose the Proc Freq output from each variable and then combine them into a single dataset.

 

I've never used Proc Report to do something like this and I'm not finding any similar examples online, so I could use some guidance.

Reeza
Super User
Are all the variables using 0/1/missing?
Wolverine
Quartz | Level 8
Spoiler

@Reeza wrote:
Are all the variables using 0/1/missing?
Yes
PaigeMiller
Diamond | Level 26

Example using SASHELP.CLASS

 

proc freq data=sashelp.class;
    tables sex/noprint missing out=percents;
run;        
proc report data=percents;
    columns sex,(count percent);
    define sex/across missing;
run;

 

The missing is not needed for variable SEX, but your example does have missings, that's why I put it in there.

 

Important concept: transpose to a wide data set is rarely useful, it makes your programming work HARDER. Why? Because almost every SAS data handling PROC expects long data sets, and doesn't work well (if at all) on wide data sets.

--
Paige Miller
Reeza
Super User

More cumbersome than needed I'm sure here.

 

data have;
    do j=1 to 100;
        array value(10);

        do i=1 to 10;
            value(i)=rand('bernoulli', 0.4);
            _missing=rand('bernoulli', 0.05);

            if _missing=1 then
                value(i)=.;
        end;
        output;
    end;
    keep j value1-value10;
run;

ods output summary=part1;

proc means data=have stackods N NMISS SUM;
    var value1-value10;
run;

data want (keep=variable N_0 percent_0 N_1 percent_1 NMISS percent_missing);
    length variable $32. N_0 percent_0 N_1 percent_1 NMISS percent_missing 8.;
    set part1;
    totalobs=NMISS+N;
    N_0=totalobs-sum-NMISS;
    N_1=sum;
    percent_0=N_0/totalobs;
    percent_1=N_1/totalobs;
    percent_missing=nmiss/totalobs;
    ;
    format percent: percent12.2;
run;

@Wolverine wrote:

I'm trying to format a frequencies output dataset. The data looks like this:

varname COUNT PERCENT

 

20 .200000000
0 50 .500000000
1 30 .300000000

 

I want it to look like this:

_NAME_ 0 PERCENT_0 1 PERCENT_1 missing PERCENT_m
varname 50 .50 30 .30 20 .20

 

Current code:

PROC TRANSPOSE data=have out=want;
	VAR varname;
	BY COUNT PERCENT; 
RUN;

 

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!

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
  • 325 views
  • 0 likes
  • 3 in conversation