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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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