BookmarkSubscribeRSS Feed
pavank
Quartz | Level 8
data cities;
    input city $;
    datalines;
chennai
mumbai
pune
chennai
mumbai
pune
;
run;

data chennai(keep=city) mumbai(keep=city) pune(keep=city);
    set cities;
    do i = 1 to 6;
        if city = 'chennai' then output chennai;
        else if city = 'mumbai' then output mumbai;
        else if city = 'pune' then output pune;
    end;
run;

Hi Experts,

Good Morning

Using above cites dataset i want split by city wise with total no of observations as per my code it gives 12 observations i want 6 observation for each city dataset in this case city dataset has 6 observations 

i didn't get how to get total observations per each individual city

required output

datasets.png

3 REPLIES 3
andreas_lds
Jade | Level 19

Each city appears twice in the data, for each observation the loop writes six obs to the city-datasets => 12 obs in each dataset.

pavank
Quartz | Level 8
data cities;
    input city $;
    datalines;
chennai
mumbai
pune
nodia
;
run;

data chennai mumbai pune noida;
    set cities nobs=nobs;
    do i = 1 to nobs;
        if city = 'chennai' then output chennai;
        else if city = 'mumbai' then output mumbai;
        else if city = 'pune' then output pune;
        else if city='nodia' then output noida;
    end;drop i;
run;

Hi @andreas_lds 

Thank you I got it 

mkeintz
PROC Star

@pavank wrote:
data cities;
    input city $;
    datalines;
chennai
mumbai
pune
nodia
;
run;

data chennai mumbai pune noida;
    set cities nobs=nobs;
    do i = 1 to nobs;
        if city = 'chennai' then output chennai;
        else if city = 'mumbai' then output mumbai;
        else if city = 'pune' then output pune;
        else if city='nodia' then output noida;
    end;drop i;
run;

You could eliminate replace the collection of IF and ELSE IF statements by using "where" parameters in the output dataset names:

data  chennai (where=(city='chennai'))  mumbai (where=(city='mumbai'))
      pune    (where=(city='pune'))     nodia  (where=(city='nodia')) ;
  set cities nobs=nobs;
  do i=1 to nobs;
    output;
  end;
  drop i;
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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
  • 3 replies
  • 833 views
  • 1 like
  • 3 in conversation