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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1260 views
  • 2 likes
  • 3 in conversation