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!
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;
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;
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
@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.
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;
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).
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;
Thank YOU!!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.