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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 6 replies
  • 691 views
  • 0 likes
  • 5 in conversation