BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
himalayancat
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

5 REPLIES 5
Astounding
PROC Star

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;

PaigeMiller
Diamond | Level 26
if not first.sex and not last.sex then do;
this='in_betwn';
end;
--
Paige Miller
ballardw
Super User

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.

 

himalayancat
Fluorite | Level 6
thank you for quick reply.was looking for what pgstats suggested.

cheers,
PGStats
Opal | Level 21

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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