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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 3469 views
  • 0 likes
  • 3 in conversation