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-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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