BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Banana19
Obsidian | Level 7

Hi all, I'm trying to increment a variable based on a macro variable. I tried to use the below code,

PROC SORT DATA=WORK.tab1;
BY ISSUE;
RUN;

DATA tab2;
SET WORK.tab1;
BY ISSUE;
RETAIN PK &MAX_PK1.;
IF FIRST.ISSUE THEN PK = &MAX_PK1. + 1;
RUN;

but it generates same number across every row. Could you please let me know what am I missing?

 

Thanks,

Banana

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Banana19 wrote:

Thanks for the reply Tom!

&max_pk1 has numeric data 15.

Expected output;

ISSUE PK
ABC1 16
ABC2 17
ABC3 18
ABC4 19

 

So whenever VARIABLE 'ISSUE' IS UNIQUE, THE PK variable should increment by 1. I'm taking the inital value as 15 so it should be populated as 15,16,17,18,19,.....


So before you try to use macro logic to generate SAS code you need to know what SAS code you need to generate.  To do what you ask you just need code like this:

data want;
  set have;
  by issue;
  retain pk 15 ;
  if first.issue and _n_>1 then pk+1;
run;

To make it dynamic just replace the 15 with the macro variable reference.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

In that code you are appear to be using the macro variable MAX_PK1 as if it contained the NAME of a variable. 

For example if MAX_PK1 has the value VAR1, like this:

%let max_pk1=VAR1;

Then the data step your macro logic generates would be:

DATA tab2;
SET WORK.tab1;
BY ISSUE;
RETAIN PK VAR1;
IF FIRST.ISSUE THEN PK = VAR1 + 1;
RUN;

If instead you have set MAX_PK1 to an actual number, like 100,

%let max_pk1=100;

then you would not want to use it in the RETAIN statement so that you could generate code like this instead.

DATA tab2;
SET WORK.tab1;
BY ISSUE;
RETAIN PK ;
IF FIRST.ISSUE THEN PK = 100 + 1;
RUN;

Either way it is obvious why PK is set to a constant. It is only set to a value on the first observation for each ISSUE group. And the value it is set to is always the same.

 

Please show sample simple example input and expected output.  You only need about two or three observations per ISSUE value for two or three different values of ISSUE. 

Banana19
Obsidian | Level 7

Thanks for the reply Tom!

&max_pk1 has numeric data 15.

Expected output;

ISSUE PK
ABC1 16
ABC2 17
ABC3 18
ABC4 19

 

So whenever VARIABLE 'ISSUE' IS UNIQUE, THE PK variable should increment by 1. I'm taking the inital value as 15 so it should be populated as 15,16,17,18,19,.....

Tom
Super User Tom
Super User

@Banana19 wrote:

Thanks for the reply Tom!

&max_pk1 has numeric data 15.

Expected output;

ISSUE PK
ABC1 16
ABC2 17
ABC3 18
ABC4 19

 

So whenever VARIABLE 'ISSUE' IS UNIQUE, THE PK variable should increment by 1. I'm taking the inital value as 15 so it should be populated as 15,16,17,18,19,.....


So before you try to use macro logic to generate SAS code you need to know what SAS code you need to generate.  To do what you ask you just need code like this:

data want;
  set have;
  by issue;
  retain pk 15 ;
  if first.issue and _n_>1 then pk+1;
run;

To make it dynamic just replace the 15 with the macro variable reference.

Banana19
Obsidian | Level 7
Thank you,Tom! It worked!!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 759 views
  • 0 likes
  • 2 in conversation