My SAS Tutorials

Friday 21 March 2014

SAS INTERVIEWS CONCEPTUAL QUESTIONS XXII



We have a SAS Data Set "ONE".


Country Month Poulation
AU          1           500
AU          5           700
US           3           800
IND         6          1000
PAK        2           600
AU          7            500
US           6           700
IND         8           1200
.               .             .
.              .              .
.             .               .
.             .               .

Suppose we have 1,00,000 observations in the SAS Data Set "ONE". We don't know how many countries are there and how many times they are repeating.

We need to create separate SAS Data Set for each country.
Most efficient program will be appreciated.

Now prepare your Base SAS and Advance SAS Certification Exams with Online Mock tests:  http://exam.sankhyana.com


For training related info kindly mail us at info@sankhyana.com
www.sankhyana.com

4 comments:

  1. Though not sure this the most efficient approach, but the below code will one create dataset for each country.

    data test;
    input country $ month population;
    datalines;
    AU 1 500
    AU 5 700
    US 3 800
    IND 6 1000
    PAK 2 600
    AU 7 500
    US 6 700
    IND 8 1200
    ;

    *Counting number of countries in the dataset;
    proc sql noprint;
    select count(distinct country) into : n from test;
    quit;
    %let n=&n;
    *Creating macro variables for each country;
    proc sql noprint;
    select distinct (country) into : c1 - : c&n from test
    order by country;
    quit;
    *Creating a macro that generates datasets;
    %macro createds(inds=test,var=country);
    %do i=1 %to &n;
    data &&c&i;
    set &inds(where=(upcase(country)="%upcase(&&c&i)"));
    run;
    %end;
    %mend creatds;
    %createds;

    ReplyDelete
  2. This code will fail if any country code contains space. After taking country code in macro variables , loop through country code and replace space by under score and store this value into another macro variable to be used as dataset name. And previously created macro variable will be used for filtering data.

    ReplyDelete
  3. data one;
    input country $ month population;
    datalines;
    AU 1 500
    AU 5 700
    US 3 800
    IND 6 1000
    PAK 2 600
    AU 7 500
    US 6 700
    IND 8 1200
    ;
    run;
    %macro location;
    %do i=1 %to &n;
    data &&country&i;
    set one(where=(upcase(country)="%upcase(&&country&i)"));
    if country = "&&country&i";
    run;
    %end;
    %mend location;
    %location;
    proc sql;
    select count(distinct country)
    into :n
    from one;
    select distinct country, month, population
    into :country1 - :country%left(&n),:month,:population
    from one;
    quit;

    ReplyDelete

  4. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    http://chennaitraining.in/base-sas-training-in-chennai/
    http://chennaitraining.in/abinitio-training-in-chennai/
    http://chennaitraining.in/datastage-training-in-chennai/
    http://chennaitraining.in/cognos-training-in-chennai/
    http://chennaitraining.in/cognos-tm1-training-in-chennai/
    http://chennaitraining.in/microstrategy-training-in-chennai/
    http://chennaitraining.in/qlikview-training-in-chennai/

    ReplyDelete