BookmarkSubscribeRSS Feed
6 REPLIES 6
Ramsha
Obsidian | Level 7

The document is very helpful however when I try to apply this or write any code, I get a message saying that the variables have already been set. 

 

It says I should do it in the DATA step however where exactly should I put the codes? 

tomrvincent
Rhodochrosite | Level 12
Somewhere after you've imported the file.
Tom
Super User Tom
Super User

Did you really start with an Excel file?  Or did you have a CSV file and your computer system "helpfully" automatically opened it in Excel instead of letting you edit it with a text editor?

 

This is what a CSV file for that type of data would look like.

Street,City,State/ZIP,Country
288 York Street,New Haven,CT 06511,USA
301-399 South Boulevard Drive,Bainbridge,GA 39819,USA
150-151 Tremont Street,Boston,MA 02111,USA
2395 Ingleside Avenue,Macon,GA 31204,USA
1007 Merchant Street,Ambridge,PA 15003,USA
859 Washington Avenue,Miami Beach,FL 33139,USA
974 Great Plain Avenue Needham MA 02492,USA, ,
139 Lynnfield Street,Peabody,MA 01960,USA
180 Nassau Street Princeton,NJ 08542,USA,
563 Carlsbad Village Drive,Carlsbad,CA 92008,USA

As you can see the line that starts with '974' is missing the comma before the STATE/ZIP value.

Ramsha
Obsidian | Level 7

Hello, thank you so much! Now I understand the issue. I previously thought csv and excel files were the same. 

So I started writing the code but I am a beginner and do not know the procedure for infile. 

Can you please tell me how I should code this to be in that format above in the question? I started with this...but of course I know my syntax is wrong. 

 

data places;
infile "/folders/myshortcuts/myfolder/places.csv" dsd delimiter=none;
input Street $ City $ State_ZIP $ Country $;
run;
proc print data=places;
run;

 

ballardw
Super User

@Ramsha wrote:

Hello, thank you so much! Now I understand the issue. I previously thought csv and excel files were the same. 

So I started writing the code but I am a beginner and do not know the procedure for infile. 

Can you please tell me how I should code this to be in that format above in the question? I started with this...but of course I know my syntax is wrong. 

 

data places;
infile "/folders/myshortcuts/myfolder/places.csv" dsd delimiter=none;
input Street $ City $ State_ZIP $ Country $;
run;
proc print data=places;
run;

 


If the file is CSV then the delimiter really better be comma : delimiter=',' .

 

If a length is not assigned to character variables before use in an input statement they will default to 8 characters.

Provide a length statement BEFORE the input statement to set the length such as

Length Street $ 100.

If your commas do not actually align with the data description you have other issues to address such as running into the end of the data line before all the values are encountered. You are reading 4 variables in the above code but if there are not 3 commas to separate them SAS may attempt to read data from the following line so you need either MISSOVER or TRUNCOVER on the infile statement. If the values may have a comma imbedded in a specific field such as street address then likely should add the DSD option as well.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 582 views
  • 1 like
  • 4 in conversation