DATA Step, Macro, Functions and more

user here with a FIRST.var & LAST.var question

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

user here with a FIRST.var & LAST.var question

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;


Accepted Solutions
Solution
a week ago
Esteemed Advisor
Posts: 5,475

Re: user here with a FIRST.var & LAST.var question

Posted in reply to himalayancat

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;
PG

View solution in original post


All Replies
Super User
Posts: 6,628

Re: user here with a FIRST.var & LAST.var question

[ Edited ]
Posted in reply to himalayancat

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;

Respected Advisor
Posts: 2,805

Re: user here with a FIRST.var & LAST.var question

Posted in reply to himalayancat
if not first.sex and not last.sex then do;
this='in_betwn';
end;
--
Paige Miller
Super User
Posts: 13,293

Re: user here with a FIRST.var & LAST.var question

Posted in reply to himalayancat

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.

 

New Contributor
Posts: 2

Re: user here with a FIRST.var & LAST.var question

thank you for quick reply.was looking for what pgstats suggested.

cheers,
Solution
a week ago
Esteemed Advisor
Posts: 5,475

Re: user here with a FIRST.var & LAST.var question

Posted in reply to himalayancat

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;
PG
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 110 views
  • 1 like
  • 5 in conversation