BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

Here are my 2 sample datasets:
Dataset1
Area_Code
1
2
3

Dataset2
Area_Code Pop Emp
1 50 10
2 100 20
3 150 30
4 200 40
5 250 50

data area_code1 ;
set dataset2;
if area_code in (1);
percent=emp/pop;
run ;

I want to know if there is a way to have 3 separate datasets by area_code (ex. Area_code1, area_code2, and area_code3). My above example only shows 3 area codes in dataset1. What if I have 100 area codes in dataset1? How do I replace the area_code in the program above without running it 100 times to perform the operation for each area code in dataset1 (1,2,3,4,etc)?

Thanks in advance for your help.
1 REPLY 1
Patrick
Opal | Level 21
Why do you want/need to create a data set per area code at first place?
This doesn't sound like a good idea.

Anyway, here a possible solution:

Data have;
infile datalines truncover dlm=' ';
input Area_Code Pop Emp;
datalines;
1 50 10
2 100 20
3 150 30
4 200 40
5 250 50
run;

proc sql noprint;
select distinct cats('area_code_',area_code), cat('when(',Area_Code,') output ',cats('area_code_',area_code),';')
into :dslist separated by ' ', :WhenList separated by ' '
from have
;
quit;

data &dslist;
set have;
percent=emp/pop;
select(area_code);
&WhenList
otherwise;
end;
run ;

Message was edited by: Patrick

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 1025 views
  • 0 likes
  • 2 in conversation