Hi,
I want to export a text file from sas which sums up certain variables of a dataset and exports it out in a format given below.
For example : balance_lc is a variable in a dataset and 9789919877.97 is the sum of this variable for all the rows in the dataset.
can you please help me in how do i sum it up and export as text file in the format given below.
See this example, using sashelp.class:
proc summary data=sashelp.class;
var age height weight;
output out=class_sum (drop=_type_ rename=(_freq_=Rowcount)) sum=;
run;
data _null_;
set class_sum;
file '$HOME/out.txt';
array vars _numeric_;
do i = 1 to dim(vars);
name = vname(vars{i});
value = vars{i};
put name "= " value;
end;
run;
The resulting text file:
Rowcount = 19 Age = 253 Height = 1184.4 Weight = 1900.5
Edit: added rowcount
See this example, using sashelp.class:
proc summary data=sashelp.class;
var age height weight;
output out=class_sum (drop=_type_ rename=(_freq_=Rowcount)) sum=;
run;
data _null_;
set class_sum;
file '$HOME/out.txt';
array vars _numeric_;
do i = 1 to dim(vars);
name = vname(vars{i});
value = vars{i};
put name "= " value;
end;
run;
The resulting text file:
Rowcount = 19 Age = 253 Height = 1184.4 Weight = 1900.5
Edit: added rowcount
Thanks a lot. Is there a way i can format just the sum of the variables to 18.6
Expand the code to use a dynamic format:
data _null_;
set class_sum;
file '$HOME/out.txt';
array vars _numeric_;
do i = 1 to dim(vars);
name = vname(vars{i});
if name = 'Rowcount'
then format = 'best.';
else format = '18.6';
value = strip(putn(vars{i},format));
put name "= " value;
end;
run;
@Kurt_Bremser wrote:
Expand the code to use a dynamic format:
data _null_; set class_sum; file '$HOME/out.txt'; array vars _numeric_; do i = 1 to dim(vars); name = vname(vars{i}); if name = 'Rowcount' then format = 'best.'; else format = '18.6'; value = strip(putn(vars{i},format)); put name "= " value; end; run;
And for slightly prettier output if this is read by people, try :
put name @25 "= " value;
as an alternative to the above put statement.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.