How do I put first and last name into one variable???

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How do I put first and last name into one variable???

data statedata;

infile '\\Client\E$\SASData\StateData.txt'


delimiter = ' ' ;

input States $ Region $ Manager Visits d z k l m n i o g;

run;

 

libname state '\\Client\E$\Data';

data state.state_final;

set statedata;

run;


proc print data=state.state_final;
run; 

 

It runs well/ however, in the first variable (states) there are states like new york and that add an extra space which throws everything off. So how do I in cases where there is spaces between the state names put them together in one in variable 1(states). 

 

Please include it in my code. Been stuck on this forever. Thank you!

 


Accepted Solutions
Solution
‎09-25-2017 12:48 PM
Regular Contributor
Posts: 194

Re: How do I put first and last name into one variable???

How about this? Smiley Happy

 

data one;

infile '\\Client\E$\SASData\StateData.txt' delimiter='|';

length inputstr rinput $200 states region $32 visits x z y $8 pos 8;

input inputstr;

rinput = strip(reverse(inputstr));

y = strip(reverse(scan(rinput,1)));

z = strip(reverse(scan(rinput,2)));

x = strip(reverse(scan(rinput,3)));

visits = strip(reverse(scan(rinput,4)));

region = strip(reverse(scan(rinput,5)));

pos = indexw(strip(inputstr),region);

states = strip(substr(inputstr,1,pos-1));

run;

View solution in original post


All Replies
Super User
Posts: 3,309

Re: How do I put first and last name into one variable???

[ Edited ]

Not sure what your data looks like but I'm guessing you need an INFORMAT modifier for your STATES variable:

 

input States : $8. Region $ Manager Visits d z k l m n i o g;

 

Occasional Contributor
Posts: 15

Re: How do I put first and last name into one variable???

I attached screenshots of what I am getting. I want the second column for the words like north dakota etc. for the second word dakota to be put in the first colum ( so as one word). Thank you

Super User
Posts: 11,578

Re: How do I put first and last name into one variable???


jsjoden wrote:

I attached screenshots of what I am getting. I want the second column for the words like north dakota etc. for the second word dakota to be put in the first colum ( so as one word). Thank you


If you must attach screenshots please use an image format like PNG or JPEG. Microsoft documents are security risks and many users here will not open them for that reason or have organization policies and/or security software that blocks them.

 

 

PROC Star
Posts: 1,801

Re: How do I put first and last name into one variable???

Since there are spaces in the first variable, how do you know where the second variable starts starts? You need another delimiter.

Either a comma, or two spaces.

& allows for single spaces in strings and waits for several spaces before reading the next variable, so may be this help.

Of course, you could have provided sample data to help us answer properly....

 

 
input STATE &: $16. REGION $ MANAGER VISITS D Z K L M N I O G;

 

 

 

 

Occasional Contributor
Posts: 15

Re: How do I put first and last name into one variable???

I attached screenshots of what I am getting. I want the second column for the words like north dakota etc. for the second word dakota to be put in the first colum ( so as one word). 

Solution
‎09-25-2017 12:48 PM
Regular Contributor
Posts: 194

Re: How do I put first and last name into one variable???

How about this? Smiley Happy

 

data one;

infile '\\Client\E$\SASData\StateData.txt' delimiter='|';

length inputstr rinput $200 states region $32 visits x z y $8 pos 8;

input inputstr;

rinput = strip(reverse(inputstr));

y = strip(reverse(scan(rinput,1)));

z = strip(reverse(scan(rinput,2)));

x = strip(reverse(scan(rinput,3)));

visits = strip(reverse(scan(rinput,4)));

region = strip(reverse(scan(rinput,5)));

pos = indexw(strip(inputstr),region);

states = strip(substr(inputstr,1,pos-1));

run;

Occasional Contributor
Posts: 15

Re: How do I put first and last name into one variable???

Thank YOU!!

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 179 views
  • 1 like
  • 5 in conversation