DATA Step, Macro, Functions and more

Using SAS functions -

Reply
Contributor afs
Contributor
Posts: 28

Using SAS functions -

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;

 

Super User
Posts: 19,769

Re: Using SAS functions -

Look at SCAN and COUNTW functions. 

 

Last_index = countw(name);

first = SCAN(name, 1);

last = scan(name, last_index);

Super User
Posts: 19,769

Re: Using SAS functions -

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

Contributor afs
Contributor
Posts: 28

Re: Using SAS functions -

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
Super User
Posts: 19,769

Re: Using SAS functions -


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. 

Trusted Advisor
Posts: 1,553

Re: Using SAS functions -

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

Contributor afs
Contributor
Posts: 28

Re: Using SAS functions -

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.
Trusted Advisor
Posts: 1,553

Re: Using SAS functions -

[ Edited ]

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

Contributor afs
Contributor
Posts: 28

Re: Using SAS functions -

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
Trusted Advisor
Posts: 1,553

Re: Using SAS functions -

[ Edited ]

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  ( )

     

     

Contributor afs
Contributor
Posts: 28

Re: Using SAS functions -

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 ...
Trusted Advisor
Posts: 1,553

Re: Using SAS functions -

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

Contributor afs
Contributor
Posts: 28

Re: Using SAS functions -

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);
Contributor afs
Contributor
Posts: 28

Re: Using SAS functions -

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 , ?

Trusted Advisor
Posts: 1,553

Re: Using SAS functions -

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

Ask a Question
Discussion stats
  • 15 replies
  • 349 views
  • 2 likes
  • 3 in conversation