create variables with certain suffix

Reply
Contributor
Posts: 36

create variables with certain suffix

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.

Respected Advisor
Posts: 3,799

Re: create variables with certain suffix

Seems like breaks are data.  Are you sure you want to put data in the meta data?

Contributor
Posts: 36

Re: create variables with certain suffix

Posted in reply to data_null__

 
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

Contributor
Posts: 36

Re: create variables with certain suffix

I found a way to do it. Thanks.

Respected Advisor
Posts: 4,173

Re: create variables with certain suffix

So how did you solve it?

Super User
Posts: 10,044

Re: create variables with certain suffix

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

Super User
Super User
Posts: 7,984

Re: create variables with certain suffix

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;

Ask a Question
Discussion stats
  • 6 replies
  • 245 views
  • 0 likes
  • 5 in conversation