Hi All,
I would like to know how to read a character variable with a multiple embedded blanks. I have the following data.
I want to create a data-set having a variable that store the above data in 9 observations. I have tried the below code.
data test; infile datalines ; length var1 $ 30.; input var1 & $ ; datalines; ISUGI 2006San Francisco CA SPHARMASUG2006Bonita SpringsFL RNESUG 2006Philadelphia PA RWUSS 2006Irvine CA RSESUG 2006Atlanta GA RSCSUG 2006Irving TX RMWSUG 2006Dearborn MI RPNWSUG 2006Seaside OR ISUGI 2007Orlando FL ; run;
Ouput:
But the char after the multiple blanks are not store in that variable.
I am not able to figure out what code to use in order to accomplish this task. Could anyone please help me by providing a code that could accomplish the above task? If you need more information, please ask. Would appreciate your help. I am using sas version 9.4
Thanks!
Vibha
data test;
/*infile datalines ;*/
/*length var1 $ 30.;*/
/*input var1 & $ ;*/
input var1 $30.;
datalines;
ISUGI 2006San Francisco CA
SPHARMASUG2006Bonita SpringsFL
RNESUG 2006Philadelphia PA
RWUSS 2006Irvine CA
RSESUG 2006Atlanta GA
RSCSUG 2006Irving TX
RMWSUG 2006Dearborn MI
RPNWSUG 2006Seaside OR
ISUGI 2007Orlando FL
;
run;
data test;
/*infile datalines ;*/
/*length var1 $ 30.;*/
/*input var1 & $ ;*/
input var1 $30.;
datalines;
ISUGI 2006San Francisco CA
SPHARMASUG2006Bonita SpringsFL
RNESUG 2006Philadelphia PA
RWUSS 2006Irvine CA
RSESUG 2006Atlanta GA
RSCSUG 2006Irving TX
RMWSUG 2006Dearborn MI
RPNWSUG 2006Seaside OR
ISUGI 2007Orlando FL
;
run;
Can you please explain why it didn't give the expected result if I provide the length by using length statement instead of specifying it in input statement ?
@Vibhaa wrote:
Can you please explain why it didn't give the expected result if I provide the length by using length statement instead of specifying it in input statement ?
With normal list mode input SAS will read up to the next delimiter (space is the default delimiter). If you add the & modifier then it will skip single delimiters and read up until two or more delimiters.
With formatted input mode SAS will read the number of characters you tell it to read. Whether they contain delimiters or not.
@Vibhaa wrote:
Can you please explain why it didn't give the expected result if I provide the length by using length statement instead of specifying it in input statement ?
The INPUT statement is what is controlling how to read the source text into the variables. It does not matter whether or not you have previously defined the variables.
If you don't define the variables before you reference them in the INPUT statement then SAS will have to guess how to define them based on how you first use them. That is why you will INPUT statements with a bare $ character after a variable. That let's SAS know that you want that variable to be character. If you have already defined the variable as character then you do not need to add the $ to the INPUT statement.
That data looks like it is in columns. I looks like there are actually five variables there.
So use column based input statement.
data test;
input type $ 1
name $ 2-10
year 11-14
city $ 15-28
state $ 29-30
;
datalines;
ISUGI 2006San Francisco CA
SPHARMASUG2006Bonita SpringsFL
RNESUG 2006Philadelphia PA
RWUSS 2006Irvine CA
RSESUG 2006Atlanta GA
RSCSUG 2006Irving TX
RMWSUG 2006Dearborn MI
RPNWSUG 2006Seaside OR
ISUGI 2007Orlando FL
;
run;
proc print; run;
Obs type name year city state 1 I SUGI 2006 San Francisco CA 2 S PHARMASUG 2006 Bonita Springs FL 3 R NESUG 2006 Philadelphia PA 4 R WUSS 2006 Irvine CA 5 R SESUG 2006 Atlanta GA 6 R SCSUG 2006 Irving TX 7 R MWSUG 2006 Dearborn MI 8 R PNWSUG 2006 Seaside OR 9 I SUGI 2007 Orlando FL
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.
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.