BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KoenN
Calcite | Level 5

Hi guys,

I'm switching to SAS from SPSS for a new project, but I struggle with converting an SPSS Macro to SAS. I was wondering if anyone could help me. This macro checks if a certain type of event (A thru Z) is ranked first, second, etc and the puts the type of event in the event_1_type and the corresponding date in event_1_date.

Current SPSS macro

DEFINE !eventrecode (nr = !CMDEND)

!DO !eventnr=1 !TO !nr

Do if A_rank = !eventnr.

  compute !CONCAT("Event_",!eventnr,"_type")=1.

  compute !CONCAT("Event_",!eventnr, "_date")=A_date.

End if.

!Doend

Execute.

!Enddefine.

!eventrecode n3=3

My SAS attempt:

%macro eventrecode(nr);

%do i=1 %to &nr;

data UT_PREPARED2;

  set UT_PREPAREd;

  if A_rank = &i then;

  SUBST("Event_"&i,"_type") = 1;

  SUBST("Event_"&i,"_date") = A_date;

  %End;

%mend eventrecode;

%eventrecode(3)

But that doesn't seem to work. Any help would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Are you just trying to do something like the following?:

%macro eventrecode(nr);

  %do i=1 %to &nr.;

    if a_rank=&i. then do;

      event_&i._type=1;

      event_&i._date=a_date;

    end;

  %end;

%mend eventrecode;

data have;

  input a_rank a_date date9.;

  cards;

1 04dec2014

1 04dec2014

2 04dec2014

2 04dec2014

3 04dec2014

3 04dec2014

4 04dec2014

4 04dec2014

;

data want;

  set have;

  %eventrecode(3)

run;

View solution in original post

4 REPLIES 4
ballardw
Super User

A description of how it "doesn't seem to work" is a good idea.

With that in mind, I suspect these lines

SUBST("Event_"&i,"_type") = 1;

  SUBST("Event_"&i,"_date") = A_date;

are generating an error and it maybe something about an undefined array reference.

The code you are using would replace the outputset NR times.

I think you can get this with array processing and if you can live slightly different variable names such as event_type_1 instead of event_1_type it gets pretty easy.

%macro eventrecode(nr);

data UT_PREPARED2;

  set UT_PREPAREd;

     array type Event_type_1 - Event_type_&nr;

     array date Event_date_1 - Event_date_&nr;

     do I = 1 to dim(type);

          if A_rank = I then do; /* the do indicates a block of statements to execute when the condition is true, you were missing this in your and only the first bit was attempting to execute*/

               type = 1;

               date = A_date;

          end; /* ends the if then do block*/

     end; /* ends loop for I*/

  run; /* very good idea to explicitly indicate the end of a data step, especially when using macros*/

  %End;

%mend eventrecode;

HINT: Before even attempting a macro make sure you have the code running as intended, the data step I indicated you could replace the two values of &nr in the body with 3 and should run.

I'm not sure if I remember the function SUBST from SPSS, but it does not exist in SAS. There are also a number of functions that perform different types of concatenation: CATS, CATT, CATX and CATQ plus other operations on character values.

KoenN
Calcite | Level 5

Thank you for your help! I'll keep it in mind to better describe 'it doesn't work' for the next time, you're absolutely right.

art297
Opal | Level 21

Are you just trying to do something like the following?:

%macro eventrecode(nr);

  %do i=1 %to &nr.;

    if a_rank=&i. then do;

      event_&i._type=1;

      event_&i._date=a_date;

    end;

  %end;

%mend eventrecode;

data have;

  input a_rank a_date date9.;

  cards;

1 04dec2014

1 04dec2014

2 04dec2014

2 04dec2014

3 04dec2014

3 04dec2014

4 04dec2014

4 04dec2014

;

data want;

  set have;

  %eventrecode(3)

run;

KoenN
Calcite | Level 5

Yes, this did the trick. The syntax is somewhat more complicatied after the if-statement, but I've just editted my original SPSS syntax and it all works. The . after &i is very neat, love it! Thank you so much!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1722 views
  • 3 likes
  • 3 in conversation