BookmarkSubscribeRSS Feed
lm12abh
Fluorite | Level 6

Hi I require some help on the below. Want is the field I wish to create, I have also put a description of what I am trying to obtain:

 

IDFLAGVAR1VAR2WANTDescription for explanation
123112.110=FIRST.ID=0
123012.120=WANT RECORD BEFORE
123212.1312.1=VAR1
123212.9412.9=VAR1
123012.9512.9=WANT RECORD BEFORE
123013.5612.9=WANT RECORD BEFORE
123114.577.5=VAR1-VAR2
456292100=FIRST.ID=0
45609780=WANT RECORD BEFORE
456097120=WANT RECORD BEFORE
456297097=VAR1-VAR2

 

Current SAS code:

data data2;
	set data1;

	by id;

	retain want;
	if first.id then want=0.00;
	else if flag=2 then want=var1;
	else if flag=1 then want=var1-var2;
	else want+0; /* without just get . , with get 0 */
run;

 

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

How does your current result differ from the wanted result you posted?

lm12abh
Fluorite | Level 6

Hi sorry I for got to mention. In the case where there is a flag=0 but the bucket before is non zero, Ishould have the same value as above. Instead I have a zero. (Issue highlighted in red).

What I have now:

IDFLAGVAR1VAR2WANTDescription for explanationHave now
123112.110=FIRST.ID=00
123012.120=WANT RECORD BEFORE0
123212.1312.1=VAR112.1
123212.9412.9=VAR112.9
123012.9512.9=WANT RECORD BEFORE0
123013.5612.9=WANT RECORD BEFORE0
123114.577.5=VAR1-VAR27.5
456292100=FIRST.ID=00
45609780=WANT RECORD BEFORE0
456097120=WANT RECORD BEFORE0
456297097=VAR1-VAR297

 

ed_sas_member
Meteorite | Level 14

Hi @lm12abh 

 

Correct me if I'm wrong but it seems that you want to display 2 decimals (e.g. not 0 but 0.00).

You can do that with a FORMAT Statement.

NB: There is no need to write want = 0.00 as it is exactly the same as want=0.

Idem for else want+0 as you have the value is already retained.

 

My best,

data data2;
	set data1;

	by id;
	format want 8.2;
	retain want;
	if first.id then want=0;
	else if flag=2 then want=var1;
	else if flag=1 then want=var1-var2;
run;

Capture d’écran 2020-02-07 à 10.43.51.pngCapture d’écran 2020-02-07 à 10.43.51.png 

lm12abh
Fluorite | Level 6

Thanks, Yes I already have a format statement (I didn't include that part of the code) good to know it wont affect it.

CFC_SAS_Communities_400x225.jpgCFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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