hi all,
i am aware little how first.var and last.var works but i want to assign value to all the var that are not in first.var and last.var but not able.
your help would be appreciated.
Thank you,
data need;
format this;
set have;
by sex;
if first.sex then do;
this='first';
end;
if not
first.sex and last.sex then do;
this='in_betwn';
end;
if last.sex then do;
this='last';
end;
/* */
/* if UPCASE(sex) NE ('F') THEN DO; */
/* YY='ZZ'; */
/* END; */
run;
Keep it simple
data need;
set have; by sex;
length this $8;
if first.sex then this='first';
else if last.sex then this='last';
else this='in_betwn';
run;
Two easy issues here ...
First, you need to set a length for your new variable. Otherwise it takes its length from "first" and won't be long enough to hold "in_betwn":
length this $ 8;
Second, NOT applies to just one condition. To get it to apply to both, add parentheses:
if not
(first.sex or last.sex) then do;
if not first.sex and not last.sex then do;
this='in_betwn';
end;
I get a "not valid file type" error attempting to open your pdf.
If the "problem" is that in between values are showing as "in_be" then the issue is that you did not assign a length to the THIS variable and so SAS assigned a length of 5 because the first use of the variable is in: This='first'; and "first" is 5 characters long.
If "this" variable exists in your set Have data set and it has a length other than 5, or is numeric that could cause other issues.
If there is something else then show the results as text and likely show the log from running the code as well.
Keep it simple
data need;
set have; by sex;
length this $8;
if first.sex then this='first';
else if last.sex then this='last';
else this='in_betwn';
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.