BookmarkSubscribeRSS Feed
Ashokburnwal
Calcite | Level 5

I have a dataset which has a variable name city which contains more than 100 city names. How can I create tables by subsetting data according to each city for all the unique city names in city variable. 

4 REPLIES 4
andreas_lds
Jade | Level 19

Do you want to create datasets or tables (a report)?

For the later: sort by "city" and use proc print with by-statement.

Ashokburnwal
Calcite | Level 5
Create datasets.
Kurt_Bremser
Super User

@Ashokburnwal wrote:
Create datasets.

And why?

Mind that in about 90% of cases, splitting a dataset is not needed.

They only time in 20+ years of SAS work where I had to split a dataset was when it grew so much that sorting it in one piece cracked my quota in UTILLOC.

Amir
PROC Star

Hi @Ashokburnwal.,

 

The following code attempts to do what you require, making use of the sashelp.class data step as input and splitting it up by sex.

 

/* use sql to construct required data set names and output logic */
proc sql noprint;
   select distinct
       cat('ds_',sex)
      ,cat('when (', quote(sex), ') output ds_', sex)
   into
       :ds_names     separated by ' '
      ,:output_logic separated by ';'
   from
      sashelp.class
   ;
quit;

options symbolgen;

/* use the macro variables created in the sql in a data step with a select statement */
data &ds_names ds_issue;
   set sashelp.class;

   select(sex);
      &output_logic;
      otherwise output ds_issue;
   end;
run;

 

 

Kind regards,

Amir.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1033 views
  • 1 like
  • 4 in conversation