BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ArchanaSudhir
Obsidian | Level 7

HI I have a dataset like this

sort1 sort2 var1 var2 var3 var4 var5 var6
1 1 5 0        
2 2 1 2 3 4 7 8 9 5 0.1 0.1 0.1 2
3 2 1 2 3 5 3 4 5 6 0.1 0.1 0.1  

 

If var2 is 0 in the first line ,

I need to set var3,var4,var5,var6 as zero in teh next 2 lines.

 

How do I do this?

 

Thanks,

Archana

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

A sum statement creates a retained variable which is useful here

 

data a;
infile datalines truncover;
input sort1	sort2	var1	var2	var3	var4	var5	var6;
datalines;
1	1	5	0	 	 	 	 
2	2	1 2 3 4	7 8 9 5	0.1	0.1	0.1	2
3	2	1 2 3 5	3 4 5 6	0.1	0.1	0.1	
;

data want;
set a;
if var2 = 0 then doit = 2;
else if doit then do;
    var3=0; var4=0; var5=0; var6=0;
    doit + (-1);
    end;
drop doit;
run;
PG

View solution in original post

4 REPLIES 4
andy_smith_amadeus_co_uk
Fluorite | Level 6

Hi Archana,

 

This looks like a job for the retain statement to set up a flag.

 

How about something like:

 

data new;

  retain delflag; * don't reset this value to missing for every iteration;

  set old;

  if _n_ eq 1 and var2=0 then delflag="Y"; * check for var2=0 only on the first iteration;

 

  if _n_ gt 1 and delflag="Y" then do;

    var3=0;

    var4=0;

    var5=0;

    var6=0; * Set to zeros only if correct condition met;

  end;

run;

PGStats
Opal | Level 21

A sum statement creates a retained variable which is useful here

 

data a;
infile datalines truncover;
input sort1	sort2	var1	var2	var3	var4	var5	var6;
datalines;
1	1	5	0	 	 	 	 
2	2	1 2 3 4	7 8 9 5	0.1	0.1	0.1	2
3	2	1 2 3 5	3 4 5 6	0.1	0.1	0.1	
;

data want;
set a;
if var2 = 0 then doit = 2;
else if doit then do;
    var3=0; var4=0; var5=0; var6=0;
    doit + (-1);
    end;
drop doit;
run;
PG
Astounding
PROC Star

It gets a little more complex if VAR2 could be 0 on consecutive observations.

LinusH
Tourmaline | Level 20
Your requirement together with the record layout gives me the feeling that you don't store the data in an optimal way.
Can you tell us the reason for this logic, and what var2 and var3-var6 represents in the real world?
Data never sleeps

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1583 views
  • 0 likes
  • 5 in conversation