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

data test;
input ID $ typ $ sub $ ;
cards;
100 1 100
105 3 100

104 2 100

107 5 100

108 4 100
110 1 200
118 3 200
;
run;


expected-

ID typ sub derived_val

100 1 100 1001
105 3 100 1003

104 2 100 1002

107 5 100 1004

108 4 100 1005

110 1 200 2001
118 3 200 2003

 

other than 1 and 2 , the other values need to be incremented by from 3.

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

I think this is what you are looking for:

 

data test;
	input ID $ typ $ sub $ ;
	cards;
	100 1 100
	105 3 100
	104 2 100
	107 5 100
	108 4 100
	110 1 200
	118 3 200
	;
run;

proc sort 
	in=test out=srtd ;
	by sub ;
run ;

data output ;
	retain counter ;
	set srtd ;
	by sub ;
	if first.sub then counter=0 ;
	if typ in ("1","2") then
		derived_val=cat(trim(sub),typ) ;
	else do ;
		if counter=0 then counter=inputn(typ,"8.") ;
		derived_val=cat(trim(sub),left(putn(counter,"8."))) ;
		counter+1 ;
	end ;
run ;

To understand this code, I suggest you check out BY group processing (first.sub) and retained values (counter) in the SAS documentation

View solution in original post

6 REPLIES 6
AMSAS
SAS Super FREQ
Hi APU_A0154665,

Can you provide more information on what you want the code to do? Specifically, how are you calculating derived_val.
APU_A0154665
Calcite | Level 5

Hey , my intention is to get the derived field incremented.

 

example -

 

ID typ sub derived_val

100 1 100 1001
105 3 100 1003

104 2 100 1002

107 5 100 1004

108 4 100 1005

110 1 200 2001
118 3 200 2003

 

if the typ is 1 then the derived_val should be as cat(sub,1) and same for 2 too.

 

But when it's 3 then the derived_val should be 1003 and then again if it's 3 then again it should be incremented by 1 and so on. The possible range for the typ is between 1 to 5.

 

hope,it's pretty clear to you .

 

Thanks!

AMSAS
SAS Super FREQ
Sorry - No it doesn't make sense to me.

If you are incrementing typ when type is 3, 4 or 5. Then I would expect your output to look like this:

ID typ sub derived_val

100 1 100 1001
105 3 100 1003
104 2 100 1002
107 5 100 1005
108 4 100 1004
110 1 200 2001
118 3 200 2004

Observation 2, is the 1st occurrence of Typ 3 so derived_val is cat(sub,typ).
Observation 7, is the 2nd occurrence of Typ 3 so derived_val is cat(sub,typ+1).
If there was another Typ 3 observation then derived_val would be cat(sub,typ+2) and so on.

From the look of your example, I think you want to actually increment if type is 3, 4 or 5, plus you want to reset when the sub changes
AMSAS
SAS Super FREQ

I think this is what you are looking for:

 

data test;
	input ID $ typ $ sub $ ;
	cards;
	100 1 100
	105 3 100
	104 2 100
	107 5 100
	108 4 100
	110 1 200
	118 3 200
	;
run;

proc sort 
	in=test out=srtd ;
	by sub ;
run ;

data output ;
	retain counter ;
	set srtd ;
	by sub ;
	if first.sub then counter=0 ;
	if typ in ("1","2") then
		derived_val=cat(trim(sub),typ) ;
	else do ;
		if counter=0 then counter=inputn(typ,"8.") ;
		derived_val=cat(trim(sub),left(putn(counter,"8."))) ;
		counter+1 ;
	end ;
run ;

To understand this code, I suggest you check out BY group processing (first.sub) and retained values (counter) in the SAS documentation

APU_A0154665
Calcite | Level 5

Thanks.

ballardw
Super User

@APU_A0154665 wrote:

Hey , my intention is to get the derived field incremented.

 

example -

 

ID typ sub derived_val

100 1 100 1001
105 3 100 1003

104 2 100 1002

107 5 100 1004

108 4 100 1005

110 1 200 2001
118 3 200 2003

 

if the typ is 1 then the derived_val should be as cat(sub,1) and same for 2 too.

 

But when it's 3 then the derived_val should be 1003 and then again if it's 3 then again it should be incremented by 1 and so on. The possible range for the typ is between 1 to 5.

 

hope,it's pretty clear to you .

 

Thanks!


Please examine the red highlighted values and explain how the derived value is assigned. It does not match your rule text.

Also, are the values text or numeric?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 2178 views
  • 0 likes
  • 3 in conversation