Reading dates into sas from premade .dat dataset - sas

Being asked to read in and format dates in a premade sas file. Question is as follows:
Read in BIRTHDATE and DEATHDATE to recognize them as dates. (Create new variables called birthdate2 and deathdate2 respectively).
So, i've created a copy of the dataset:
data presidents_datefix;
set myfolder.presidents1;
run;
And am trying to use the input function to read the birth and deathdates in:
data presidents_datefix;
infile "presidents1"
input Presidents $ 1-38 #2 Birthdate date9. Deathdate date9.;
run;
Before running this datastep, the presidents1_datefix viewtable loads just fine. After the datastep, I get a warning message that reads:
"WARNING: The data set WORK.PRESIDENTS_DATEFIX may be incomplete. When this step was stopped there were 0 observations and 0 variables.
WARNING: Data set WORK.PRESIDENTS_DATEFIX was not replaced because this step was stopped."
Then, when opening the viewtable, no observations exist anymore. I also get a series of error messages that say
ERROR 23-2: invalid option name
for every value of the input function.
Would really appreciate some help figuring out what I've done wrong and how I can fix it! Thank you!

Related

Variable has never been referenced [SAS]

I have a dataset containing information by an account number. I'm trying to add a new variable, called product_type, populated with the same value for every record. It is within a SAS macro.
data CC_database;
set cc_base_v2 (keep=accnum date product_type);
product_type="CC";
where date>=%sysevalf("&start_date."d) and file_date<=%sysevalf("&end_date."d);
run;
However, I keep getting the error "The variable product_type has in the DROP, KEEP, or RENAME list has never been referenced" and the output dataset only shows a blank column called product_type. What is happening here?
You are using the KEEP= dataset option on the input dataset. So the error is saying PRODUCT_TYPE does not exist in the dataset CC_BASE_V2.
If you want to control what variables are written by the data step just use a KEEP statement.
keep accnum date product_type;
In your case you could use the KEEP= dataset option but only list the variables taht are coming from CC_BASE_V2.
data CC_database;
set cc_base_v2(keep=accnum date file_date);
where date>= "&start_date."d and file_date<="&end_date."d;
product_type="CC";
drop file_date;
run;

Generating new Variable in SAS results in ERROR 180-322

I am very new to SAS, which is why this question has probably a quite easy answer. I use the SAS university edition.
I have dataset containing socio-structural data in 31 variables and 1000000 observations. The data is stored in a Stata .dta file, which is why I used the following code to import in into SAS:
LIBNAME IN '/folders/myfolders/fake_data';
proc import out= fake_2017 datafile = "/folders/myfolders/fake_data/mz_2017.dta" replace;
run;
Now, I want to create new variables. At first, a year variable that takes the value 2017 for all observations. After, I have several other variables that I want to generate from the 31 existing variables. However, running my code I get the same error message for all my steps:
year = 2017;
run;
ERROR 180-322: Statement is not valid or it is used out of proper order.
I found many things online but nothing that'd help me. What am I doing wrong/forgetting? For me, the code looks like in all the SAS tutorial videos that I have already watched.
You cannot have an assignment statement outside of a data step. You used a PROC IMPORT step to create a dataset named fake_2017. So now you need to run a data step to make a new dataset where you can create your new variable. Let's call the new dataset fixed_2017.
data fixed_2017;
set fake_2017;
year=2017;
run;

What does the colon mean when located after the set statement, in SAS?

I have the following code and I don’t know what SAS is doing here.
data have;
set folder.pst:;
if.....
run;
Now, there are several datasets that are named ”pst201812”, ”pst201901” and ”pst201902” in the libname called folder. Does the colon in the code above mean that ALL the datasets starting with pst are read by SAS? Or have I misunderstood?
You got it right. The Set Statement with the Colon operator reads all datasets that begin with pst in this case. See a small example below and read the "Using Data Set Lists with SET" section of the Set Statement Documentation.
data pst201812;a=1;run;
data pst201901;a=2;run;
data pst201902;a=3;run;
data want;
set work.pst:;
run;

how to read date and time from txt file using sas

I'm very new to SAS and I'm trying to read a txt file that contains date and time. The file is shown in the following figure
I believe I have tried all the possible options that I can think of to read the file but the output is still in numeric form. The following is the code I'm using
data wb_bg_1619;
infile "C:\Users\daizh\Desktop\Ren\SAS\wb_bg_0215.txt" firstobs=3 missover;
informat DATE DATE7. TIME TIME5. ;
input DATE TIME BG;
run;
proc print data=wb_bg_1619;
run;
The output looks like this
You've used an informat to automatically convert a date stored as text into a numeric SAS date format, which is the number of days since Jan 1 1960. To display that in a human readable format, you need to use a regular format. Add the following to the top or bottom of your code:
format date date9.
time time.
;
This changes how the data is displayed to you, but does not change how SAS works with it. As far as SAS is concerned, a date is only a number. You could run the rest of your program without ever using a format and get the right numbers and calculations with it if you wanted to, but that sure makes troubleshooting hard.
To remember the difference between a format and informat:
informats are for inputs
formats are for you

Can I get some default/empty text to display if a PROC REPORT doesn't generate due to no valid data?

I have a SAS program that loops through certain sets of data and generates a bunch of reports to an ODS HTML destination.
Sometimes, due to small sets of data I run these reports for, a certain PROC REPORT will not generate because, for this set of data I'm on, there is no data to report. I get this message for those instances:
WARNING: A GROUP, ORDER, or ACROSS variable is missing on every observation.
What I want in the HTML is to display some sort of message for these like "did not generate" or something.
I tried to use return/error codes or the warning text above to detect this, but the error code is 0 (no problem, really?) and the warning text doesn't reset if the next PROC REPORT generates OK.
If it is of any importance, I'm using a data step with CALL EXECUTE to get all this PROC REPORT code generated for these sets of data.
Is there any way to generate this "did not generate" message or at least to catch these warnings per PROC REPORT?
You can substitute in a value for the missing observations in your report.
First redefine missing values to some character. I think you can only use a single character, I could be wrong, though.
options missing='M';run;
Then make sure to use the "missing" option in your PROC REPORT.
proc report data=somedata nowd headline missing;
....
run;
EDITS BASED ON COMMENTS
To get comments to show up, I see a few possibilities.
One, scan the the data set and check for missing values. If any are present throw a message out.
Data _Null_;
Set dataset;
file print notitles;
if obs = . then do;
put #01 'DID NOT COMPUTE';
stop;
end;
run;
Two, add a column with a compute:
define xx /computed "(Message)";
compute xx /char length=16 ;
if obs =. then xx = 'did not compute value in row';
Three, a conditional line using compute:
compute after obs;
if obs = . then do;
line #1 "DID NOT COMPUTE";
end;
endcomp;
endcomp;
See: http://www2.sas.com/proceedings/sugi26/p095-26.pdf
Look for the MTANYOBS macro and the section on printing a 'no observations' page.