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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 477 views
  • 0 likes
  • 3 in conversation