1
votes

I'm importing a data set in Stata format using PROC IMPORT, but I'd like to use IF statements to create a few new variables as well. However, simply using code like this returns an error of "ERROR 180-322: Statement is not valid or it is used out of proper order." :

PROC IMPORT
    DATAFILE = "myfile.dta"
    DBMS = DTA
    OUT = mydata
    REPLACE;
IF close < 10 THEN val = "low";
ELSE val = "high";
RUN;

I don't see any code for a data step generated in my log file either, even if I use ODS TRACE ON. Is there another way to use IF statements here?

UPDATE: If I add a DATA step after the PROC IMPORT, as shown:

PROC IMPORT
        DATAFILE = "myfile.dta"
        DBMS = DTA
        OUT = mydata
        REPLACE;
RUN;

DATA mydata;
    IF close < 10 THEN val = "low";
    ELSE val = "high";
RUN;

PROC PRINT DATA=mydata;
RUN;

This only prints out an empty table like this:

close | val
      | high

and inspecting the dataset shows that it doesn't contain any of the original data.

2
Just do a data step immediately after the proc import....Jay Corbett
@CarolinaJay65 How would that DATA step be structured? Do I still need an INFILE statement in that DATA step? If I just put a DATA step with an IF statement, it appears to delete the data (see my edit).Ricardo Altamirano

2 Answers

4
votes

As others have stated, you cannot use an if statement inside of a proc import. You are on the right track by placing the if inside a subsequent data step - you just had your syntax a little wrong. It should look like this:

DATA mydata2;    /* A NEW DATASET CALLED MYDATA2 WILL BE CREATED. NOTE THERE IS NO EQUALS SIGN */
    set mydata;  /* IT WILL BE BASED OFF THE MYDATA DATASET */
    IF close < 10 THEN val = "low";
    ELSE val = "high";
RUN;
1
votes

You can never use IF in a SAS procedure. IF belongs into a data step.

update

proc import ...;
run;

data mydata;
set mydata;
length val $4; **ensure right length;
if close<10 then val='low';
else val='high';
run;