DATA Step, Macro, Functions and more

Proc Import and formatting

Reply
Regular Contributor
Posts: 180

Proc Import and formatting

%let DorsHHFPath =/dorsshare/HHFit;

%put &DorsHHFPath;

proc import out= al_ 

datafile="&DorsHHFPath/al/*.csv"

dbms=CSV replace ;

getnames=no;

datarow=3

run;

 

proc import out= ga 

datafile="&DorsHHFPath/ga/*.csv"

dbms=CSV replace ;

getnames=no;

datarow=3

run;

 

The results are two datasets by state with the following:
var1     var2        var3
State    Company     Date
Al        MyCompany  1/23/18
Al        MyComapny  1/31/18

 

A similar dataset for GA.  I do a separate proc import for each state (15 in all)

1.I have to use a rename for all of the vars (ie rename var1 = State) 

2. In addition, Date imports as a character.  Can I define it as a date9. in the initial proc import statement? Also is there a way to define the other vars based on the names directly under it (ie var2 = company)

3.  In each row, the state will repeat itself depending on the number of entries for that state.  Is there a way to only bring in data and say delete the row if var1(state) does not equal the state I choose (ie AL)?

 

 

Super User
Posts: 24,028

Re: Proc Import and formatting

1. ok, not a question here.

2. Use a data step to import the data rather than PROC IMPORT, then you can control the variable names, lengths and types. First figure it out for one and then you can make a macro. You can get the data step code from the log and change it to what you need manually first.

3. Sure, just add an IF statement to your data step code.

 

Untested, but since you've clearly been working on this for a while this may help you get started:

 

%macro import_data_states(state=);

data STATE_&state.;
infile "&dorshhfpath./&state./*.csv" dsd truncover;
input ..... ;
if state = "&state";
run;

%mend;

data _null_;
set sashelp.states; *not sure of which data set this is because I don't have maps portion installed;

str = catt('%import_data_states(state=', stateName, ');');

call execute(str);
run;

%*combine after if desired;
data All_States;
set state_: indsname=source;
dsn=source;
run;

Ask a Question
Discussion stats
  • 1 reply
  • 79 views
  • 0 likes
  • 2 in conversation