IF Statement Macros

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

IF Statement Macros

Hi Everyone,

I am still quite new to SAS and would like to ask question. Thanks in advance.

What do I have?

Col1

Col2

Col3

OBS

1.5

2

3.5

1

1.5

2

3.5

2

1.5

2

3.5

3

1.5

2

3.5

4

There are 26 observations and 26 Columns (COL1-COL26) and one OBS (observation number). Here I just present a snapshot of my variable.

What do I want?

Col1

Col2

Col3

OBS

0

2

3.5

1

1.5

0

3.5

2

1.5

2

0

3

1.5

2

3.5

4

Basically I want to have 0 on the diagonals where the OBS number is equal to the column number.

Macros that I used:

%let n_PPG=26;

%macro price(n_PPG);

%do na=1 %to 26;

data data.own2; set data.own1;

%if observ=&na %then %do;

COL&na=0;

%end;

run;

%end;

%mend;

%Price(&n_PPG);

When I ran this, I did not get what I want (it does not change a thing). Can anyone help? Thanks.


David


Accepted Solutions
Solution
‎08-31-2015 07:48 AM
Super User
Posts: 6,982

Re: IF Statement Macros

data want;

set have;

array cols {*} col1-col26;

cols{_n_} = 0;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 5,260

Re: IF Statement Macros

Hmm. What is your "real" source? And why do want to do with this?

Looks like you are trying to use SAS as if it was Excel or something?

If you want to it a data driven way:

  • Assign an id variable. In your case just use data step: id = _n_;
  • Transpose your data, now having Id, col no and value.
  • Assign value to 0 if id = col no.
Data never sleeps
Contributor
Posts: 21

Re: IF Statement Macros

Thank you Linus. I have 370,000+ data which limit me to do it in EXCEL. I tried your way with the macros but it failed to work. But it seems that KurtBremser's way is working fine.

Again, thanks for the speedy support.

Best,
David

Solution
‎08-31-2015 07:48 AM
Super User
Posts: 6,982

Re: IF Statement Macros

data want;

set have;

array cols {*} col1-col26;

cols{_n_} = 0;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 21

Re: IF Statement Macros

Thank you KurtBremster. You are legend!


Best,


David

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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