Hi,
How to creates a series variables that have certain suffix? For example, I have macros
%let breaks = 28 32 41 50; * breaks of segments;
%let segvarprefix = ageseg_; * new variable name prefix for the segments;
How do I create the series of variables of
ageseg_28 ageseg32 ageseg_41 ageseg_50?
Thank you very much.
Seems like breaks are data. Are you sure you want to put data in the meta data?
Thanks for your reply. Here is some more details of my code. I have the input data as following.
data indat;
input age;
datalines;
34
42
30
;
run;
I am going to write a macro, 'segment_var(indata, outdata, breaks, segvarprefix)', to outputs a dataset which has some new variables that indicates which segment the input value falls into. If using the dataset above and execute the macro
%segment_var(indata = indat, outdata = outdat, breaks = 28 32 41 50, segvarprefix = ageseg_);
I should get the output data, 'outdat', looking like
age ageseg_28 ageseg_32 ageseg_41 ageseg_50
34 0 1 1 0
42 0 0 1 1
30 1 1 0 0
I found a way to do it. Thanks.
So how did you solve it?
Maybe this :
%let breaks = 28 32 41 50; * breaks of segments; %let segvarprefix = ageseg_; * new variable name prefix for the segments; %let vars= ; %macro gen; %do i=1 %to %sysfunc(countw(&breaks)); %let vars= &vars &segvarprefix%scan(&breaks,&i); %end; %put &vars ; %mend gen; %gen
Xia Keshan
Wasn't entirely sure what the segments were here - how does an age fit into more than one segment? Anywas, by datastep:
data indat;
input age;
datalines;
34
42
30
;
run;
data _null_;
array segment{4,2} $200. ( "0", "28",
"29", "32",
"33", "41",
"42", "50");
call execute('data want; set indat;');
do i=1 to 4;
call execute(' attrib ageseg_'||segment{i,2}||' format=best.;
if '||segment{i,1}||' <= age < '||segment{i,2}||' then ageseg_'||segment{i,2}||'=1; else ageseg_'||segment{i,2}||'=0; output;');
end;
run;
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.
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.