BookmarkSubscribeRSS Feed
anmolk171
Calcite | Level 5

proc import
datafile = "/home/anmolk1710/sasuser.v94/Ultrasound_1.csv"
out = UBData
dbms=csv
replace;
run;

 

 

data UBData;
set UBData;
predictVar = Pass/Fail;
drop Pass/Fail;
run;

 

This is throwing error

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Try using

 

options validvarname=any;

and then you can refer to this variable as

 

'Pass/Fail'n

 

Note the quotes around the variable name followed by the letter n

 

Even better:

 

options validvarname=any;
proc datasets library=work nolist;
    modify ubdata;
    rename 'pass/fail'n = predictvar;
run; quit;
options validvarname=v7;

 

 

 

--
Paige Miller
s_lassen
Meteorite | Level 14

I think that if you set the option VALIDVARNAME=V7 before you do the PROC IMPORT, your variable will be called something that does not require quotes, probably "Pass_Fall".

 

You can also do the rename in the actual PROC IMPORT, instead of having a separate step afterwards (shown with VALIDVARNAME=ANY):

proc import
datafile = "/home/anmolk1710/sasuser.v94/Ultrasound_1.csv"
out = UBData(rename=('Pass/Fall'n=predictVar))
dbms=csv
replace;
run;
s_lassen
Meteorite | Level 14

Yes, @Kurt_Bremser is right. When it comes to .csv-files, it is much better to use data-step code than PROC IMPORT. You do not have to write the whole data step yourself, though. After submitting PROC IMPORT, you can use the RECALL command once in a text editor window, or scrape the generated data step code from the log window. You will then have not the PROC IMPORT code, but the data step code that PROC IMPORT generated. Adapt that changing the names of variables (and possibly other stuff), and save the code. You will then have something that gives similar data type results every time, even if the input data varies (PROC IMPORT guesses the types of the variables by looking at the file).

ballardw
Super User

@s_lassen wrote:

Yes, @Kurt_Bremser is right. When it comes to .csv-files, it is much better to use data-step code than PROC IMPORT. You do not have to write the whole data step yourself, though. After submitting PROC IMPORT, you can use the RECALL command once in a text editor window, or scrape the generated data step code from the log window. You will then have not the PROC IMPORT code, but the data step code that PROC IMPORT generated. Adapt that changing the names of variables (and possibly other stuff), and save the code. You will then have something that gives similar data type results every time, even if the input data varies (PROC IMPORT guesses the types of the variables by looking at the file).


If your data source is nice there should be some documentation as to maximum length of character variables, how date, time or datetime values will appear, and such. So you can adjust the generated code to match so later files with different values don't get truncated.

One recurring theme: identification variables consisting of digits: account numbers, bank routing numbers, part numbers  and such likely should be character values. Otherwise you may lose things like important leading zeroes or exceed the storage precision of numbers.

Kurt_Bremser
Super User

Do not use PROC IMPORT for a csv (or any text) file. Write the DATA step yourself, so you have full control over the import process.

You can force PROC IMPORT to use reasonable names by issuing

options validvarname=v7;

before the procedure. But you will still have to fix other things later which are caused by the guessing of PROC IMPORT.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 647 views
  • 1 like
  • 5 in conversation