Hello All,
i've a dataset as mention below...
data t;
input SUBJID Num;
datalines;
121 23
121 24
121 21
121 22
121 18
121 42
121 44
;
run;
My requirement is if i compare first two record then the smallest value would come into new variable. if i compare first 3 records then smallest value would come ... same way i have to take for others.
Output needed as below:
SUBJID Num NewVar
121 23
121 24 23
121 21 21
121 22 21
121 18 18
121 42 18
121 44 18
Thank you in advance....
Looks like you want a running (cumulative) minimum. But why doesn't the first observation have 23 as the minimum? Without that requirement it is really easy.
data want;
set t;
newvar=min(newvar,num);
retain newvar;
run;
Does your actual data have multiple values of SUBJID? Do you want to restart the min calculations when starting a new subject?
data want;
set t;
by subjid ;
newvar=min(newvar,num);
if first.subjid then newvar=.;
output;
if first.subjid then newvar=num;
retain newvar;
run;
data t;
input SUBJID Num;
newvar=min(num,lag(num));
datalines;
121 23
121 24
121 21
121 22
121 18
121 42
121 44
;
run;
@PaigeMiller Thank you for quick response:
If i've more then 6 records , in this case it's not working.
data t;
input SUBJID Num;
newvar=min(num,lag(num));
datalines;
121 23
121 24
121 21
121 22
121 18
121 42
121 45
121 56
;
run;
121 23 23
121 24 23
121 21 21
121 22 21
121 18 18
121 42 18
121 45 42
121 56 45
For future reference, saying it is "not working" never provides enough information for us to provide any help. What is not working? Please explain. Please provide details.
If I've only total number of records is 6 , in this case i'm getting correct output. If I've more than 6 ....in my case i took 9 records... i'm not getting expected output.
Below is the output :
Subjid Num newvar
121 23 23
121 24 23
121 21 21
121 22 21
121 18 18
121 42 18
121 45 42
121 56 45
expected output:
Subjid Num newvar
121 23 23
121 24 23
121 21 21
121 22 21
121 18 18
121 42 18
121 45 18
121 56 18
In the code you provided earlier in message #3 in this thread, there are 8 input observations. My program produces 8 output observations, and is different than the output you show (and I think my code produces the correct answers).
So, show your code.
Looks like you want a running (cumulative) minimum. But why doesn't the first observation have 23 as the minimum? Without that requirement it is really easy.
data want;
set t;
newvar=min(newvar,num);
retain newvar;
run;
Does your actual data have multiple values of SUBJID? Do you want to restart the min calculations when starting a new subject?
data want;
set t;
by subjid ;
newvar=min(newvar,num);
if first.subjid then newvar=.;
output;
if first.subjid then newvar=num;
retain newvar;
run;
Hi @singhsahab Please change your subject line to a more descriptive one. "Base SAS" means what?
data t;
input SUBJID Num;
datalines;
121 23
121 24
121 21
121 22
121 18
121 42
121 44
;
run;
data want;
set t;
by subjid;
retain want;
if first.subjid then want=.;
want= min(want,num);
run;
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.
Ready to level-up your skills? Choose your own adventure.