DATA Step, Macro, Functions and more

How to read a character variable with a multiple embedded blanks.

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to read a character variable with a multiple embedded blanks.

Hi All, 

 

I would like to know how to read a character variable with a multiple embedded blanks. I have the following data.

 

multiple_blanks.png

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:multiple_blank2.png

 

 

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

 

 

 


Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 1,592

Re: How to read a character variable with a multiple embedded blanks.

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;

View solution in original post


All Replies
Solution
2 weeks ago
PROC Star
Posts: 1,592

Re: How to read a character variable with a multiple embedded blanks.

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;
New Contributor
Posts: 3

Re: How to read a character variable with a multiple embedded blanks.

Posted in reply to novinosrin

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 ?

Super User
Super User
Posts: 7,938

Re: How to read a character variable with a multiple embedded blanks.


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

 

Super User
Super User
Posts: 7,938

Re: How to read a character variable with a multiple embedded blanks.


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

 

Super User
Super User
Posts: 7,938

Re: How to read a character variable with a multiple embedded blanks.

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
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 107 views
  • 2 likes
  • 3 in conversation