How do I get my SexAgeInfo dataset to output all my information in the csv file using informat?

Reply
Occasional Contributor
Posts: 13

How do I get my SexAgeInfo dataset to output all my information in the csv file using informat?

Hello All, 

 

How do I get my SexAgeInfo dataset to output all my information using informat? I have attached the csv file below so you'll know what the output should look like in sas output. Here's my sas code that I am struggling with.  

 

libname Project '/folders/myfolders';

data Project.SexAgeInfo; 
infile '/folders/myfolders/68380_example/SexAgeInfo.csv';    
informat Gender $5.  
         Characteristic $17.
         NumberWorkers2015 $8.
         NumberWorkers2016 $8.
         MedianEarnings2015 $5.
         MedianEarnings2016 $5.;
input Gender 
      Characteristic 
      NumberWorkers2015 
      NumberWorkers2016 
      MedianEarnings2015 
      MedianEarnings2016;
run; 

title "SexAgeInfo DataSet"; 
proc print data=project.sexageinfo; 
run; 

 

 

 

 

 

 

Super User
Posts: 22,874

Re: How do I get my SexAgeInfo dataset to output all my information in the csv file using informat?

Are you trying to import data? You talk about 'output' which usually means output from SAS rather than importing data into a system...

Assuming you're trying to import data:

 

1. You missed specifying the delimiter and/or the DSD option on the INFILE statement

2. You're trying to read numeric variables as characters...which is ultimately your choice, but if you plan to do any calculations or summary statistics you'll eventually need to convert them to numeric variables.

3. Your first row is the variable name so you should also uses the FIRSTOBS option to specify your data starts on the second line. 

 

 

I would strongly advise to take the easy route on this. 

 

1. Run a PROC IMPORT on the data and let it generate the code. 

2. Review the log with the code and see what changes you need to make to yours to get it to work. 

 

The PROC IMPORT code looks like:

 

proc import out=want datafile='path to your csv file.csv' dbms=csv replace; run;

 

 

 

 

 

Super User
Posts: 9,611

Re: How do I get my SexAgeInfo dataset to output all my information in the csv file using informat?

Look at my suggestion in https://communities.sas.com/t5/General-SAS-Programming/How-can-I-get-rid-of-unnecessary-rows-when-I-.... Add a

firstobs=2

option in the infile statement (your original example of the csv did not have a header line).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,084

Re: How do I get my SexAgeInfo dataset to output all my information in the csv file using informat?

A CSV file does not show what data "should look like in sas". CSV files do not have any data types but SAS differentiates between numeric and character with dates, times and datatime values specially valued numeric variables.

 

If your code, which looks reasonable, isn't providing the result you expect tell us why not.

 

I suspect that the informat you have specified for

         NumberWorkers2015 $8.
         NumberWorkers2016 $8.
         MedianEarnings2015 $5.
         MedianEarnings2016 $5.;

are incorrect as you are using CHARACTER informats, that is what the $ means in this use.

 

I think you might want to use COMMA6. for the Number variables to capture numeric values and best8. for the Median variables if you are looking for numeric values.

Ask a Question
Discussion stats
  • 3 replies
  • 161 views
  • 0 likes
  • 4 in conversation