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

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.

 

 

bw_mrd.JPG

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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 

laksh_05
Calcite | Level 5

Thanks a lot. Is there a way i can format just the sum of the variables to 18.6

Kurt_Bremser
Super User

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;
ballardw
Super User

@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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 4 replies
  • 822 views
  • 2 likes
  • 3 in conversation