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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 932 views
  • 0 likes
  • 5 in conversation