BookmarkSubscribeRSS Feed
jcis7
Pyrite | Level 9
proc sql;
create table want as
select 'A' label='Variable',
 (select sum(A) from have) as All,
 (select sum(A) from have where SCHTYPE='PUBLIC') as Public,
 (select sum(A) from have where SCHTYPE='PRIVATE') as Private
from have(obs=1)
union 
select 'B' label='Variable',
 (select sum(B)from have) as All,
 (select sum(B)from have where SCHTYPE='PUBLIC') as Public,
 (select sum(B)from have where SCHTYPE='PRIVATE') as Private
from have(obs=1)
union
select 'C' label='Variable',
 (select sum(C) from have) as All,
 (select sum(C) from have where SCHTYPE='PUBLIC') as Public,
 (select sum(C) from have where SCHTYPE='PRIVATE') as Private
from have(obs=1)
union 
select 'D' label='Variable',
 (select sum(D)from have) as All,
 (select sum(D)from have where SCHTYPE='PUBLIC') as Public,
 (select sum(D)from have where SCHTYPE='PRIVATE') as Private
from have(obs=1)
union
select 'E' label='Variable',
 (select (sum(A)/sum(B))*100 from have) as All, 
 (select (sum(A)/sum(B))*100 from have where SCHTYPE='PUBLIC') as Public,
 (select (sum(A)/sum(B))*100  from have where SCHTYPE='PRIVATE') as Private
from have(obs=1)
union 
select 'F' label='Variable',
  (select (sum(C)/sum(D)*100 from have) as All,
 (select (sum(C)/sum(D)*100  from have where SCHTYPE='PUBLIC') as Public,
 (select (sum(C)/sum(D)*100  from have where SCHTYPE='PRIVATE') as Private
from have(obs=1)

;
quit;

Hi,

 

 

I have the code above and want to export the table into Excel with the percentage values for F and G rounded to the tenths. Is it possible to do so?

 

Thank you!

9 REPLIES 9
Reeza
Super User

 


jcis7 wrote: 

I have the code above and want to export the table into Excel with the percentage values for F and G rounded to the tenths. Is it possible to do so?

 

Thank you!


Yes, it's possible. 

 

What exactly is your question?

jcis7
Pyrite | Level 9
How do you do it? Thanks.
Reeza
Super User

http://blogs.sas.com/content/sasdummy/2012/02/11/export-excel-methods/

 

For the percents, apply a PERCENT format. 

 

select 'A' label='Variable',
 (select sum(A) from have) as All,
 (select sum(A) from have where SCHTYPE='PUBLIC') as Public format=percent8.1,
 (select sum(A) from have where SCHTYPE='PRIVATE') as Private format=percent8.1
from have(obs=1)

 

 

jcis7
Pyrite | Level 9

Thanks.  The formatting doesn't stay when the table is exported. 

Reeza
Super User

That depends on how you export it. 

Use ODS EXCEL to maintain the formats. 

Or use PUT to convert the data to a character variable with the format.

 

char_var = put(old_var, percent8.1);
SASKiwi
PROC Star

Please clarify: do you want to maintain your data at full precision but just display the values as rounded, or do you want to actually round the data as stored? If it's the latter then use @Tom's solution.

Tom
Super User Tom
Super User

If you want to round the results use the ROUND() function.

 

select round(sum(C)/sum(D)*100 from have),0.1) as All  
     , ...
jcis7
Pyrite | Level 9

Thank you everyone!

 Two questions:

 

I just need the value rounded to the tenth exported into an excel worksheet.  I tried Tom's code and got the following error when I used the following code.

1.  What am I doing wrong? 

2.  What does (obs=1) do?

Any help you can give will be most appreciated.  Thank you!

 

proc sql;
create table want as
select 'C.' label='A-G',
  (select round(sum(A)/sum(F) from a.have),0.1) as All, 
 (select round(sum(A)/sum(F)   from a.have),0.1) where pub_priv='PUBLIC') as Public,
 (select round(sum(A)/sum(F)   from a.have),0.1) where pub_priv='PRIVATE') as Private
from a.have(obs=1);
quit;

 

 

 

 

 

771

772 proc sql;

773 create table want as

786 select 'C. label='A-G',

787 (select round(sum(A)/sum(F) from a.have),0.1) as All,

---- -

22 76

202

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, ), *, **, +, ',', -, /, <,

<=, <>, =, >, >=, ?, AND, BETWEEN, CONTAINS, EQ, EQT, GE, GET, GT, GTT, IN, IS, LE,

LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, ^, ^=, |, ||, ~, ~=.

ERROR 76-322: Syntax error, statement will be ignored.

ERROR 202-322: The option or parameter is not recognized and will be ignored.

787! (select round(sum(A)/sum(F) from a.have),0.1) as All,

-

22

ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &, *, **, +,

',', -, /, <, <=, <>, =, >, >=, ?, AND, AS, BETWEEN, CONTAINS, EQ, EQT, FORMAT, FROM,

GE, GET, GT, GTT, IN, INFORMAT, INTO, IS, LABEL, LE, LEN, LENGTH, LET, LIKE, LT, LTT,

NE, NET, NOT, NOTIN, OR, TRANSCODE, ^, ^=, |, ||, ~, ~=.

788 (select round(sum(A)/sum(F) from a.have),0.1) where pub_priv='PUBLIC') as

788! Public,

789 (select round(sum(A)/sum(F) from a.have),0.1) where pub_priv='PRIVATE') as

789! Private

790 from a.have(obs=1);

791 quit;

NOTE: The SAS System stopped processing this step because of errors.

NOTE: PROCEDURE SQL used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

Reeza
Super User

You need to add a n after the first name. Which is why there's a best practice to not allow spaces and symbols in the name.

 

select 'C.'n label='A-G',

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 1188 views
  • 0 likes
  • 4 in conversation