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
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;
o what is your question?
What have you tried?
Explore the other truncation functions...
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;
Or did you have something else in mind regarding your header "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;
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 😎 then evn_odd = <even_message>;
else even_odd = <odd_message>;
enter the full text instead <even_message> or <odd_message>
Thanks sir
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.