0

I am looking to create multiple datasets from city_variables dataset. There are a total of 58 observations that I summed up into macrovariable (&count) to stop the do loop.

The city_variables dataset looks like (vertically ofcourse):

CITY_NAME
City1
City2
City3
City4
City5
City6
City7
City8
City9
City10

..........

City58

I created macrovariable &name from a data null statement in order to input the cityname into the dataset name.

Any help would be great on how to automate the creation of the 48 files by name (not number). Thanks again.

/Create macro with number of observations in concordinate file/

proc sql;
select count(area_name);
into :count
from main.state_all;
quit;


%macro repeat;
data _null_;
set city_variables;
%do i= 1 %UNTIL (i = &count);  

call symput('name',CITY_NAME);
run;

data &name;
set dataset;
where city_name = &name;
run; 

%end;
%mend repeat;
%repeat

2 Answers 2

1

Well, if you're going to do

proc sql;
select count(area_name);
into :count
from main.state_all;
quit;

Then why not go all the way? Make a macro that does one dataset output, given the criteria as parameters, then make one call for each separate whatever-name. This might be close to what you're looking at.

%macro make_data(data_name=, set_name=, where=);

data &data_name.;
  set &set_name.;
  where &where.;
run;

%mend make_data;

proc sql;
   select
     cats('%make_data(data_name=',city_name,
          ',  set_name=dataset, where=city_name="',
           city_name,
           '" )')
      into :make_datalist
      separated by ' '
    from main.state_all;
quit;


&make_datalist.;

Some other options that I'll just link to:

Chris Hemedinger @ SAS Dummy blog How to Split One Data Set Into Many shows a similar concept except he doesn't put the macro wrapper where I do.

Paul Dorfman, Data Step Hash Objects as Programming Tools is the seminal paper on using a hash table to do this. This is the "fastest" way to do this, likely, if you understand hash tables and have the memory available.

7
  • 1
    I would note that ~90% of the time someone asks this question, they shouldn't; they should be using by instead and not splitting their dataset up. But YMMV.
    – Joe
    Apr 28, 2017 at 21:37
  • Thanks for your input. I will give this a try.
    – Randy
    May 1, 2017 at 12:14
  • I got an error when attempting this: ERROR: PROC SQL only allows missing arguments for COMPRESS and SCAN. It does not allow a missing argument for function CATS.
    – Randy
    May 1, 2017 at 13:12
  • Ah, I Think there's an extra comma.
    – Joe
    May 1, 2017 at 16:26
  • Thanks that got it working. To add more criteria to the query where within or after where=city_name="'
    – Randy
    May 1, 2017 at 18:21
0

You don't need to use a macro to automate splitting up your data in this way. Since your example is really simple, I would consider using call execute in a null data step:

data test;
    infile datalines ;
    input city_name $20.;
    datalines;
City1
City2
City2
City3
City3
City3
;
run;

data _null_;
    set test;
    call execute("data "||strip(city_name)||";"||"
    set test;
    where city_name = '"||strip(city_name)||"';"||"
    run;");
run;
4
  • Thanks using this method. How do I go about tacking on more logic to this statement? between ; and last ; (second where statement)
    – Randy
    May 1, 2017 at 17:55
  • @Randy Call execute is just executing a string that you pass it - so you would just need to add any logic by concatenating more statements into this string you are building. For example that where line could be: where city_name = '"||strip(city_name)||"' and population > 10000;"||" which would resolve to something like "where city_name = 'City1' and population > 10000;"
    – John
    May 2, 2017 at 2:41
  • Thanks John. Brilliant example. I have been hesitant implementing the use of call execute, but now I can see the many uses of this command.
    – Randy
    May 2, 2017 at 13:15
  • @Randy Thanks - if this answered your question would you accept it as the answer?
    – John
    May 2, 2017 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.