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
Each city appears twice in the data, for each observation the loop writes six obs to the city-datasets => 12 obs in each dataset.
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
@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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.