How to create nested rows in SAS? - sas

I am a beginner so my knowledge is lacking. I have a data set consisting of the following columns:
Subject
Age
Height
Weight
I wish to create a table such that for every 1 person i have three rows called Age, Height, Weight.
I have tried to use Proc Tabulate :
proc tabulate data=new;
class person;
var NEWCOLUMN;
table person,NEWCOLUMN;
run;
However i am getting an error because the new column is not the correct type.

You can pivot the data per person and REPORT or TABULATE the cell values.
Example:
proc transpose data=sashelp.class out=pivoted;
by name;
var age height weight;
where name <= 'C';
run;
ods html file='output.html' style=plateau;
options nodate nonumber nocenter;
title; footnote;
proc report data=pivoted;
column name _name_ col1;
define name / ' ' order order=data;
define _name_ / ' ';
define col1 / ' ';
run;
proc tabulate data=pivoted;
class name _name_ / order=data;
var col1;
table name*_name_='', col1=''*min='' / nocellmerge;
run;
ods html close;
Output

Related

Weighted mean in SAS

I have problem with some SAS code. Within first weighted mean grouping by "date", I want to compute again weighted mean using "group" with by option and "w2" as weight. How can I do this?
proc univariate data=set_out;
by date;
weight w1;
VAR price;
run;
The weight statement accepts only one variable, so you will need to use UNIVARIATE twice:
proc sort data=have;
by date;
proc univariate data=have;
by date;
weight w1;
VAR price;
output out=want mean=mean_price;
run;
and
proc sort data=have;
by group;
proc univariate data=have;
by group;
weight w2;
VAR price;
output out=want mean=mean_price;
run;
If you don't want to sort the data use a CLASS statement instead of BY

How specify GBP currency in proc tabulate format

What is the format for GBP currency?
I would like to have the variable 'rent' to be displayed as £X in the below tabulate. What format should I put in the 'format' statement?
An example would be the equivalent of 'format=dollar12' that gives the USD currency. What would it be for GBP?
proc tabulate format=?;
var rent;
class bedrooms city;
table bedrooms, (city=' ' all='Total')*rent=' '*mean=' ' / box='Average Rent';
run;
Apply a format to the mean statistic using * f= NLMNLGBP.
From https://communities.sas.com/t5/General-SAS-Programming/How-do-I-specify-a-currency-format-and-a-comma-format-in-a-DATA/td-p/80230
applied NLMNLGBP. to the currency field, which formatted it as £1,672,349
Sample code
data have;
bedrooms=2; city='London'; rent=4500;
label rent = 'Rent';
format rent NLMNLGBP.;
run;
proc tabulate data=have;
class bedrooms city;
var rent;
table
bedrooms
,
(city=' ' all='Total')
* rent=' '
* mean=' ' * f=NLMNLGBP. %* <----- Happy brexiting ;
/
box='Average Rent'
;
run;

How to PROC PRINT only the sum of a column of data

In SAS, you can use PROC PRINT to sum a column and display the sum:
proc print data = dataset.project_out;
sum variable;
run;
How can I get this function to only print the sum line and not the rest of the data?
I don't think you can do it with proc print. The closest you can come is the empty var statement:
proc print data=sashelp.class;
var ;
sum age;
run;
But sum adds the sum variable to the var list.
You can certainly accomplish this a number of other ways.
PROC SQL is the one I'd use:
proc sql;
select sum(Age) from sashelp.class;
quit;
PROC REPORT, often called "pretty PROC PRINT", can do it also:
proc report data=sashelp.class;
columns age;
define age/analysis sum;
run;
PROC TABULATE can do it:
proc tabulate data=sashelp.class;
var age;
tables age*sum;
run;
PROC MEANS:
proc means data=sashelp.class sum;
var age;
run;
Etc., plenty of ways to do the same thing.

Tabulate\Ods.ExcelXP - Use tagattr on var and style on class

So here, I am losing the TagAttr on the highlighted "All" row. What I want is the Total row to be highlighted and to also maintain the TagAttr on the output.
ods tagsets.excelxp
file= "c:\Sheet1.xml"
proc tabulate data=sashelp.class missing;
class sex age;
var height;
table (sex='' all='Total')*
(age=''
all={label='Total NCO' s={background=yellow font_weight=bold}} *{s={font_weight=bold background=yellow }}),
(height='Height'*mean=' ' *[s=[tagattr='#,##0;[Red](#,##0);"-"']]
height='%height'*pctsum=''*[s=[tagattr='0.00%;[Red]0.00%;"-";"-"']])
/ box=_page_ style_precedence=row;
run;
ods tagsets.excelxp close;

Transpose output of PROC UNIVARIATE:TestsForLocation

I want to format the result of the following call to PROC UNIVARIATE (TestForLocation).
proc sort data=sashelp.class; by sex; run;
proc univariate data = sashelp.class mu0 = 1;
ods select TestsForLocation;
var age;
by sex;
ods output TestsForLocation=ttest;
run;
data ttest; set ttest; keep sex test stat pvalue;run;
proc print data=ttest;run;
How can I trasnpose the output into a datset with the following columns?
Obs, Sex, StudentsT_Stat, StudentsT_pValue, SignedRank_Stat, SignedRank_Pvalue
You need to double transpose here. Make a dataset with 12 observations, with four columns: Obs, Sex, ID being the combination of Test and (Stat|pValue) that you want as your eventual variable name, and Value being the value you want transposed into the variable. Then,
proc transpose data=ttest_double out=ttest_transposed;
by obs sex;
id ID;
var Value;
run;
(ID and Value can be any variable name you like.)