BookmarkSubscribeRSS Feed
afs
Calcite | Level 5 afs
Calcite | Level 5

Want to use variables first and last names from my data set to compute name in the form first, one space, last

Want to call firstlast assigning length. Then using strip function.

Then i want to use name in the form last, a comma, one space, first. Calling it lastfirst and assigning appropriate length. and using strip function .

Data is :

obs      Name                                first             last

1     Rahul David Anand                Rahul         Anand 
2     David  Tel Merck   Boxer        David        Boxer
3     Jim  carney Ramsey              Jim            Ramsey
4     George Uday Yale                George      Yale
5     Frank  Bates                          Frank        Bates
6    Stacy Mary kim  Coghlan       Stacy          Coghlam

 

my code which i am trying is not giving me accurate results :I would be grateful if anybody can assist.

 

length firstlast $50 ;
firstlast=catx(' ',first,last);

first=strip(first);
last=strip(last);
run;

 

length lastfirst $50;
lastfirst=catx(' ',last,',',first);

first=strip(first);
last=strip(last);

run;

 

15 REPLIES 15
Reeza
Super User

Look at SCAN and COUNTW functions. 

 

Last_index = countw(name);

first = SCAN(name, 1);

last = scan(name, last_index);

Reeza
Super User

You're not using CATX properly. Review the documentation and the examples in the documentation. Post back if you can't get it working. 

afs
Calcite | Level 5 afs
Calcite | Level 5
I asked a simple question. I know that i can read the documentation. Why you are replying like i am kid and don't know where to read and what to read. I just wanted help to get required results. I know every thing about scan and countw.And what you have suggested will never work.Let somebody else help me and you stay easy.Thanks
Reeza
Super User

@afs wrote:
I asked a simple question. I know that i can read the documentation. Why you are replying like i am kid and don't know where to read and what to read. I just wanted help to get required results. I know every thing about scan and countw.And what you have suggested will never work.Let somebody else help me and you stay easy.Thanks

I interpreted your question incorrectly, but your response is childish. 

 

Try using ', ' as the first argument to CATX for your second step. 

Good luck and good bye. 

Shmuel
Garnet | Level 18

What are your results and why do you think it is not accurate ?

afs
Calcite | Level 5 afs
Calcite | Level 5
Dear Shmuel ,
When i run the first code its seems ok , but when i run the second part - the log gives error in length ,
I have tried scan , but it doesnt work.
Shmuel
Garnet | Level 18

You have not post your results and what error you have.

I can't run your code now.

 

what is the error message ? usually SAs messages are clear enough to overcome.

 

You can use an alternative code:

 

firstlast = strip(first} || ' ' || strip(last);

 

lastfirst = strip(last) || ',' || strip(first);

afs
Calcite | Level 5 afs
Calcite | Level 5
Dear Shmuel, When i write code as below as i need to define new variable firstlasta nd lastfirst , but log give error

length firstlast $50 lastfirst $50;
firstlast = strip(first} || ' ' || strip(last);
lastfirst = strip(last) || ',' || strip(first);
run;
log error:
29 firstlast = strip(fitst} || ' ' || strip(last);
_______
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

30
31 lastfirst = strip(last) || ',' || strip(first);
_________
180
Shmuel
Garnet | Level 18

The message:  Statement is not valid or it is used out of proper order.

menas that code is out of proper order. It should be like:

 

data want;

  set have;

      ... enter any code ...

run;

 

It seems that you submitted lines without the first two lines ?!

 

I got the error - you used different brackets :  ( }      instead  ( )

     

     

afs
Calcite | Level 5 afs
Calcite | Level 5
I actually found the error. The code you send had parenthesis error. That was a typing error. It worked. Actually, i was using the start lines , i was just sending you the main codes. Thanks for the help ...
Shmuel
Garnet | Level 18

your code should work too. @Reeza was right:

 

length lastfirst $50;
/*  lastfirst=catx(' ',last,',',first); <<< replace with next line */

lastfirst(',',last,first);

first=strip(first);
last=strip(last);

afs
Calcite | Level 5 afs
Calcite | Level 5
That was my working , he gave the following, and guided me to study from the documentation. His code did not work. And if someone wants to guide , he should not show attitude . I am head of a company,,,and just learning some new stuff so wanted guidance......I appreciate for your help...Still a long way to complete the codes ..Will get back if need help

Look at SCAN and COUNTW functions.

Last_index = countw(name);
first = SCAN(name, 1);
last = scan(name, last_index);
afs
Calcite | Level 5 afs
Calcite | Level 5

Dear Shmuel,

Just wanted to know if like in the data set there is a person who has 4 names and need to get his first middle name like David Tel Merck Boxer so Tel is his middle name , and there are people who have only two names so no middle is there or we can say there would be blank. So i want to create a variable middle with length $25 .So is that also possible , ?

Shmuel
Garnet | Level 18

You can use function COUNTW (meaning: Count words) and then check and decide 

what to assign to the new variable:

 

if countw(names) > 2 then mid_name = scan(names,2);

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 15 replies
  • 1388 views
  • 2 likes
  • 3 in conversation