BookmarkSubscribeRSS Feed
swimmer
Calcite | Level 5

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.

6 REPLIES 6
data_null__
Jade | Level 19

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

swimmer
Calcite | Level 5

 
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

swimmer
Calcite | Level 5

I found a way to do it. Thanks.

Patrick
Opal | Level 21

So how did you solve it?

Ksharp
Super User

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1879 views
  • 0 likes
  • 5 in conversation