How to display even observations without using mod function, if statement and loops

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

How to display even observations without using mod function, if statement and loops

Hi!!! please help me out to get output for this input without using mod function, if statement and loops

Input:

110 111 101 113 109

108 105 101 109 101

122 123 124 125 101

127 128 129 130 103

102 103 101 104 107

106 107 101 109 103

101 109 107 109 107

111 101 999 105 101

Output:


108 105 101 109 101

127 128 129 130 103

106 107 101 109 103

111 101 999 105 101


Accepted Solutions
Solution
‎08-04-2013 10:03 PM
Super User
Super User
Posts: 7,039

Re: How to display even observations without using mod function, if statement and loops

Is this a game?  Do I get a cookie if I win?

data have;

  input x1-x5 ;

cards;

110 111 101 113 109

108 105 101 109 101

122 123 124 125 101

127 128 129 130 103

102 103 101 104 107

106 107 101 109 103

101 109 107 109 107

111 101 999 105 101

run;

data want;

  link getone;

getone:

  set have;

run;

data _null_;

set want;

put (x1-x5) (Smiley Happy ;

run;


108 105 101 109 101

127 128 129 130 103

106 107 101 109 103

111 101 999 105 101

NOTE: There were 4 observations read from the data set WORK.WANT.

View solution in original post


All Replies
Super User
Posts: 5,498

Re: How to display even observations without using mod function, if statement and loops

Sounds like a short homework assignment, so it's possible you may need to look up and study some of the tools used here:

data want;

  do _n_=2 to _nobs_ by 2;

     set have nobs=_nobs_ point=_n_;

     output;

  end;

  stop;

run;

Good luck.

Occasional Contributor
Posts: 10

Re: How to display even observations without using mod function, if statement and loops

Posted in reply to Astounding

Thanks Astouding its working fine.

But I need code to get output without using mod function, if statements and loops.

Super User
Super User
Posts: 7,039

Re: How to display even observations without using mod function, if statement and loops

If your source is a text file (or inline card images) then just skip a line in the INPUT statement.

data want;

  input / x1-x5 ;

cards;

110 111 101 113 109

108 105 101 109 101

122 123 124 125 101

127 128 129 130 103

102 103 101 104 107

106 107 101 109 103

101 109 107 109 107

111 101 999 105 101

run;

Super Contributor
Posts: 297

Re: How to display even observations without using mod function, if statement and loops

DATA WANT;

SET HAVE;

IF _N_/2 = INT(_N_/2);

RUN;

Respected Advisor
Posts: 4,920

Re: How to display even observations without using mod function, if statement and loops

There are many ways to interpret that question. So I will choose the easiest one Smiley Happy. Here you are: no MOD, no loop, no IF :

data _null_;

input;

input;

put _infile_;

datalines;

110 111 101 113 109

108 105 101 109 101

122 123 124 125 101

127 128 129 130 103

102 103 101 104 107

106 107 101 109 103

101 109 107 109 107

111 101 999 105 101

;

PG

PG
Occasional Contributor
Posts: 10

Re: How to display even observations without using mod function, if statement and loops

Thanks PGStats for reply.

I am newbie to SAS. I tried your code but its not working.

Respected Advisor
Posts: 4,920

Re: How to display even observations without using mod function, if statement and loops

It is for me. What error do you get?

PG
Respected Advisor
Posts: 3,799

Re: How to display even observations without using mod function, if statement and loops

What about the "data step loop".  Can't do much without loops.

Respected Advisor
Posts: 4,920

Re: How to display even observations without using mod function, if statement and loops

If the data resides in a dataset then you have to resort to conditionals other than IF and conditions not involving MOD such as :

data have;
input;
line = _infile_;
datalines;
110 111 101 113 109
108 105 101 109 101
122 123 124 125 101
127 128 129 130 103
102 103 101 104 107
106 107 101 109 103
101 109 107 109 107
111 101 999 105 101
;

data want(where=(line ne "DROP"));
set have;
line = choosec(1+(round(_n_, 2) = _n_), "DROP", line);
run;

data want2;
set have;
select (round(_n_, 2));
     when (_n_) output;
     otherwise;
     end;
run;

PG

PG
Solution
‎08-04-2013 10:03 PM
Super User
Super User
Posts: 7,039

Re: How to display even observations without using mod function, if statement and loops

Is this a game?  Do I get a cookie if I win?

data have;

  input x1-x5 ;

cards;

110 111 101 113 109

108 105 101 109 101

122 123 124 125 101

127 128 129 130 103

102 103 101 104 107

106 107 101 109 103

101 109 107 109 107

111 101 999 105 101

run;

data want;

  link getone;

getone:

  set have;

run;

data _null_;

set want;

put (x1-x5) (Smiley Happy ;

run;


108 105 101 109 101

127 128 129 130 103

106 107 101 109 103

111 101 999 105 101

NOTE: There were 4 observations read from the data set WORK.WANT.

Respected Advisor
Posts: 4,920

Re: How to display even observations without using mod function, if statement and loops

Tom, you certainly deserve that cookie! - PG

PG
Respected Advisor
Posts: 3,156

Re: How to display even observations without using mod function, if statement and loops

It took me a while to figure out that implicit "return" in action. Thanks, Tom.

Occasional Contributor
Posts: 10

Re: How to display even observations without using mod function, if statement and loops

Thanks a lot. You really deserve the cookie Smiley Wink.

Thanks a lot to everyone for wonderful ideas.

Respected Advisor
Posts: 3,156

Re: How to display even observations without using mod function, if statement and loops

No MOD, No IF and No Loop:

proc sql;
    select * from have
  where round(monotonic()/2)=monotonic()/2;
  quit;
🔒 This topic is solved and locked.

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

Discussion stats
  • 14 replies
  • 354 views
  • 6 likes
  • 7 in conversation