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.
Do you want to create datasets or tables (a report)?
For the later: sort by "city" and use proc print with by-statement.
@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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.