BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

how to sum all numeric variables in sashelp.class write do loop in sql 

 

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Why do you want to limit yourself to SQL? There are other procedures better suited for the job. Check out Proc Means or Proc Summary

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

SQL server is not the same as SAS.

Let SAS do the heavy work and then push the results to the SQL server.

 

Cynthia_sas
Diamond | Level 26

Hi:

  SAS Procedures like PROC MEANS and PROC TABULATE (and others) support the use of _NUMERIC_ and _CHARACTER_ to deal with all the numeric or all the character variables. I do not believe you can use this syntax shortcut in PROC SQL.

 

  Using SAS procedures you don't need a do loop at all:

no_do_loop_needed.png

 

  And PROC REPORT would do this too. But  I would probably start with PROC MEANS.

 

Using SQL, you don't need a do loop, but you do need to know the names of the variables:

proc sql;
  select sum(age) as agesum, sum(height) as htsum, sum(weight) as wtsum
  from sashelp.class;
quit;

 

Hope this helps,

 

Cynthia

s_lassen
Meteorite | Level 14

I think @Cynthia_sas already gave you a good answer, but you should also know that there is no such thing as a "do loop" in SQL. This is because SQL is a declarative language (you tell the computer what you want), rather than a procedural language (you tell the computer what to do) like SAS datastep or macro language.

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
  • 4226 views
  • 1 like
  • 5 in conversation