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

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
FredrikE
Rhodochrosite | Level 12

How about this? 🙂

 

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

7 REPLIES 7
SASKiwi
PROC Star

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;

 

jsjoden
Obsidian | Level 7

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

ballardw
Super User

@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.

 

 

ChrisNZ
Tourmaline | Level 20

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;

 

 

 

 

jsjoden
Obsidian | Level 7

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

FredrikE
Rhodochrosite | Level 12

How about this? 🙂

 

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;

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
  • 7 replies
  • 3820 views
  • 1 like
  • 5 in conversation