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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.