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

Hi there.

If I have a data like below,

Data have;
Input A1 A2 A3 A4;
Cards;
1 2 3 4
1 3 4 2
1 2 3 2
2 3 2 3
2 3 4 2
2 4 5 3
3 2 4 3
3 3 4 2
;
Run;

 

I'd like to make a condition that, if S1=1, delete S2's value which is in same row of S1=1.

 

So the result should be like this.

Data have2;
Input A1 A2 A3 A4;
Cards;
1 . 3 4
1 . 4 2
1 . 3 2
2 3 2 3
2 3 4 2
2 4 5 3
3 2 4 3
3 3 4 2
;
Run;

And please let me know the way I can keep the values of S2 where value of S1=1, but remove others.

It should be like this.

 

Data have3;
Input A1 A2 A3 A4;
Cards;
1 2 3 4
1 3 4 2
1 2 3 2
2 . 2 3
2 . 4 2
2 . 5 3
3 . 4 3
3 . 4 2
;
Run;

 

Thank you for the great help and advice in advance!

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Data step is fine.

 

Data have;
Input A1 A2 A3 A4;

if A1=1 then A2=.;
Cards;
1 2 3 4
1 3 4 2
1 2 3 2
2 3 2 3
2 3 4 2
2 4 5 3
3 2 4 3
3 3 4 2
;
Run;

With proc sql you could do

 

proc sql;
    UPDATE have
    SET A2=.
    WHERE A1=1;
quit;

View solution in original post

7 REPLIES 7
gamotte
Rhodochrosite | Level 12

Hello,

 

Delete A2 when A1=1 :

 

if A1=1 then A2=.;

 or

if A1=1 then call missing(A2);

 

Delete A2 when A1 != 1 :

 

if A1 ne 1 then A2=.;

 

MonoLight
Obsidian | Level 7

Thank you!

Should I use Data? or Sql? (I'm not familiar with Sql though)

gamotte
Rhodochrosite | Level 12

Data step is fine.

 

Data have;
Input A1 A2 A3 A4;

if A1=1 then A2=.;
Cards;
1 2 3 4
1 3 4 2
1 2 3 2
2 3 2 3
2 3 4 2
2 4 5 3
3 2 4 3
3 3 4 2
;
Run;

With proc sql you could do

 

proc sql;
    UPDATE have
    SET A2=.
    WHERE A1=1;
quit;
MonoLight
Obsidian | Level 7

Wow, Thank you gamotte!

I really appreciate it!

I didn't know I can write condition before input.

 

Thank you again!

MonoLight
Obsidian | Level 7
 

If I want to delete A2's values if A1 is not 1,

Can I just use this?

 

proc sql;
    UPDATE have
    SET A2=.
    WHERE A1 != 1;
quit;

 

And actually I'm trying to delete A2s if A1's value is certain characters.

It keeps making error with != ...

gamotte
Rhodochrosite | Level 12
"!=" is not a valid operator in SAS. Use "ne" or "^=".
andreas_lds
Jade | Level 19

@MonoLight wrote:

Thank you!

Should I use Data? or Sql? (I'm not familiar with Sql though)


A data-step. in my eyes sql-code is almost always ugly mess.

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