Help using Base SAS procedures

Macro or Array

Reply
N/A
Posts: 0

Macro or Array

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.
Respected Advisor
Posts: 4,173

Re: Macro or Array

Posted in reply to deleted_user
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
Ask a Question
Discussion stats
  • 1 reply
  • 118 views
  • 0 likes
  • 2 in conversation