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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 508 views
  • 1 like
  • 4 in conversation