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

Hi,

I am trying to subset dataset based on the ranges in the dataset. I have done subsetting using Data step as :

 

DATA lt_15k lt_20k lt_30k lt_40k gt_40k;
set sas1.cars;
if price le 15 then Output lt_15k;
if (15<price<=20) then Output lt_20k;
if (20<price<=30) then Output lt_30k;
if (30<price<=40) then Output lt_40k;
if (price>40) then output gt_40k;

run;

 

I want same results by using Proc SQL command. Is possible to create multiple dataset using proc sql.

1 ACCEPTED SOLUTION

Accepted Solutions
Thamaraikannan
Fluorite | Level 6

It is not possible to produce multiple tables or views in a single sql query.

 

Best way could be :

proc sql;
create table lt_15k as select * from sas1.cars where price le 15 ;
create table lt_20k as select * from sas1.cars where price between 15 and 20 ;
create table lt_30k as select * from sas1.cars where price between 20 and 30 ;
create table lt_40k as select * from sas1.cars where price between 30 and 40 ;
quit;

 

View solution in original post

2 REPLIES 2
Thamaraikannan
Fluorite | Level 6

It is not possible to produce multiple tables or views in a single sql query.

 

Best way could be :

proc sql;
create table lt_15k as select * from sas1.cars where price le 15 ;
create table lt_20k as select * from sas1.cars where price between 15 and 20 ;
create table lt_30k as select * from sas1.cars where price between 20 and 30 ;
create table lt_40k as select * from sas1.cars where price between 30 and 40 ;
quit;

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You cannot in SQL.  I would however question the benefitof splitting the data in the first place.  First off you are multiplying the size of the size as you have each of the header blocks in addition, so this method takes more space.  It is also harder to program with as you need to know the datasets, and program for each of them.  A simpler methodology is to apply the grouping in the data, and then use that grouping.  Say you want to print each of those groups to a diffrent page:

DATA lt_15k lt_20k lt_30k lt_40k gt_40k;
set sas1.cars;
if price le 15 then Output lt_15k;
if (15<price<=20) then Output lt_20k;
if (20<price<=30) then Output lt_30k;
if (30<price<=40) then Output lt_40k;
if (price>40) then output gt_40k;
run;
title "Group1 "; Proc print data=lt_15k_20k; run;
title "Group2"; proc print data=...

Or, and this should look simpler:

data want;
  set sas1.cars;
  if price le 15 then group="lt_15k";
  if (15<price<=20) then group="t_20k";
  if (20<price<=30) then group="lt_30k";
  if (30<price<=40) then group="lt_40k";
  if (price>40) then group="gt_40k";
run;

proc print data=want;
  by group;
  title "Group: #byval1";
run;

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!

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