DATA Step, Macro, Functions and more

using _n_

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

using _n_

I have:

  data even_odd;

   input x @@;

   datalines;

   257779  269998  11  20

   ;

   run;

Modify the following data step to create a new variable named as message. The values of the

       variable message must be:

      ‘The integer is odd’ if the variable x in the input statement has odd integer, ‘The integer is even’

       if the variable x has even integer.

 

Should not use  mod function


Accepted Solutions
Solution
‎11-13-2016 10:49 PM
Respected Advisor
Posts: 3,887

Re: using _n_

Like so often there are multiple ways to get it done.

proc format;
  value even_odd
    0='The integer is odd' 
    1='The integer is even'
  ;
quit;

data even_odd;
  input x @@;
  format even_odd even_odd.;
  even_odd= (int(x/2)=x/2);
  datalines;
   257779  269998  11  20
   ;
run;

View solution in original post


All Replies
Super User
Posts: 5,255

Re: using _n_

o what is your question?

What have you tried?

Explore the other truncation functions...

Data never sleeps
Frequent Contributor
Posts: 89

Re: using _n_

question isi have variable x
23
456
78889
23 is odd a new variable message to be creted as odd .Similarlly
456 is even message has to be created even
PROC Star
Posts: 549

Re: using _n_

The most elegant way would be to use the MOD function, but since you rule this out, this is a possibility:

 

data even_odd;
   input x @@;
   datalines;
   257779 269998 1120
   ;
run;

data want(keep = x evenodd);
   set even_odd;
   y = put(x, 10.);
   
   lastdigit = substr(y,length(y),-1);

   if lastdigit in ('0', '2', '4', '6', '8') then evenodd = 'Even';
   else evenodd = 'Odd';
run;
PROC Star
Posts: 549

Re: using _n_

Or did you have something else in mind regarding your header "Using _N_" ? 

Solution
‎11-13-2016 10:49 PM
Respected Advisor
Posts: 3,887

Re: using _n_

Like so often there are multiple ways to get it done.

proc format;
  value even_odd
    0='The integer is odd' 
    1='The integer is even'
  ;
quit;

data even_odd;
  input x @@;
  format even_odd even_odd.;
  even_odd= (int(x/2)=x/2);
  datalines;
   257779  269998  11  20
   ;
run;
Trusted Advisor
Posts: 1,370

Re: using _n_

If not using MOD function then you can check the right most digit:

 

string = left(x); /* convert x from numeric to characters */

digit = input(substr(string, length(string),1),1.);

if digit in ( 0 2 4 6 8) then evn_odd = <even_message>;

else even_odd = <odd_message>;

 

enter the full text instead <even_message> or <odd_message>

Frequent Contributor
Posts: 89

Re: using _n_

Thanks sir

Super User
Posts: 9,671

Re: using _n_

Interesting question.


  data even_odd;
   input x @@;
   y=x-int(x/10)*10;
   if y in (0 2 4 6 8) then msg='Even';
    else msg='Odd ';
   datalines;
   257779  269998  11  20
   ;
   run;



☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 320 views
  • 4 likes
  • 6 in conversation