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

Hello. When I import a file there's an automatic var1 that appears, but when I try to use a formatting statement it doesn't use the observations in the first variable, instead it reads nothing and erases the observations in the first variable, as well as the name, but there's still a space for it.

 

Here is the code I'm using:

Proc import datafile= 'Y:\SAS\Political_Party.txt'
out= politicalparty
replace;
getnames=no;
run;

/this gives me all the observations in the set/;
data mylib.politicalparty;
set politicalparty;
run;

 

data work.politicalparty;
set politicalparty;
Length
gender $1
party $2
vote 3
foreign 4
spend 5;
label
gender= 'Gender'
party='Political Party'
vote='Did you vote in the last election?'
foreign="Do you agree with the government's foreign policy?"
spend= 'Should we increase domestic spending?';
run;

 

Thank you for reading.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

There is no reason to use PROC IMPORT to read a text. Especially if you the file does not have even a header row.

 

So just start with a data step that reads the file.  So something like this. The LENGTH of a SAS variable is how many byte sit takes in the SAS dataset, it has almost nothign to do with how many characters it takes to display it as text in file. SAS stores numbers as 8 byte floating point values.  So there is no reason the set lengths of numeric variables to anything other than 8.

 

So your program might just be something like this:

data mylib.politicalparty;
  infile 'Y:\SAS\Political_Party.txt' dsd dlm='|' truncover ;
  length
    gender $1
    party $2
    vote 8
    foreign 8
    spend 8
  ;
  input gender -- spend ;
  label 
    gender= 'Gender'
    party='Political Party'
    vote='Did you vote in the last election?'
    foreign="Do you agree with the government's foreign policy?"
    spend= 'Should we increase domestic spending?'
  ;
run;

View solution in original post

6 REPLIES 6
ballardw
Super User

It would be best to copy a few lines from the text file paste them into a code box opened on the forum using the </> icon. The code box is important because that will preserve the file format better. The main message windows will modify text.

 

The code you use with the getnames=no is possibly an issue if you expect to get variable names from the column headers. That option says not to.

Proc import datafile= 'Y:\SAS\Political_Party.txt'
out= politicalparty
replace;
getnames=no;
run;

 

Diana__
Fluorite | Level 6

Thank you very much for replying so quickly!

There are no names at the top of the text document I'm importing, if I don't use that statement then I only get 12/14 of the observations. There are no missing values or anything, an example of how it looks after the import is here (It's on a different computer so I'm sorry for the lack of pictures):

 

(obs. #) l    VAR1      l Gender l (etc.)

1           l 007MR110 l              l

2           l 013FD101 l               l

 

Note: I have added the ID to the beginning now, it covers rows 1-3. It now has the data in the first column, but not any of the others.

ballardw
Super User

@Diana__ wrote:

Thank you very much for replying so quickly!

There are no names at the top of the text document I'm importing, if I don't use that statement then I only get 12/14 of the observations. There are no missing values or anything, an example of how it looks after the import is here (It's on a different computer so I'm sorry for the lack of pictures):

 

(obs. #) l    VAR1      l Gender l (etc.)

1           l 007MR110 l              l

2           l 013FD101 l               l

 

Note: I have added the ID to the beginning now, it covers rows 1-3. It now has the data in the first column, but not any of the others.


We need to know what the file looks like BEFORE importing the file since your issue involves reading the file. To do that we need actual text of the file. Pictures do not work as we can't write to code to read a picture.

That means open the file with a plain text editor of some sort, such as NotePad, copy 4 or 5 lines of text and then on the forum open a code box using the </> icon and then paste the text. The code box is critical because the message windows will reformat text and often removes blanks and inserts html tags so that what is in a main window is NOT what is in your file.

 

PGStats
Opal | Level 21

Add the statement

 

DELIMITER = '|';

 

to proc import.

PG
AMSAS
SAS Super FREQ
Your example file looks a little odd, the delimiters you are using are "l" (lower case letter L) at least in the post.
Still what PGStats suggests is correct you need to define the delimiter be that a lower case L or whatever the delimiter is.
Tom
Super User Tom
Super User

There is no reason to use PROC IMPORT to read a text. Especially if you the file does not have even a header row.

 

So just start with a data step that reads the file.  So something like this. The LENGTH of a SAS variable is how many byte sit takes in the SAS dataset, it has almost nothign to do with how many characters it takes to display it as text in file. SAS stores numbers as 8 byte floating point values.  So there is no reason the set lengths of numeric variables to anything other than 8.

 

So your program might just be something like this:

data mylib.politicalparty;
  infile 'Y:\SAS\Political_Party.txt' dsd dlm='|' truncover ;
  length
    gender $1
    party $2
    vote 8
    foreign 8
    spend 8
  ;
  input gender -- spend ;
  label 
    gender= 'Gender'
    party='Political Party'
    vote='Did you vote in the last election?'
    foreign="Do you agree with the government's foreign policy?"
    spend= 'Should we increase domestic spending?'
  ;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 499 views
  • 2 likes
  • 5 in conversation