BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
singhsahab
Lapis Lazuli | Level 10

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....

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller
singhsahab
Lapis Lazuli | Level 10

@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

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
singhsahab
Lapis Lazuli | Level 10

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

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Tom
Super User Tom
Super User

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;
novinosrin
Tourmaline | Level 20

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;
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
  • 7 replies
  • 1799 views
  • 0 likes
  • 4 in conversation