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 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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