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.
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
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!
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
Thanks.
@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 1003104 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?
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!
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.