Generating new Variable in SAS results in ERROR 180-322 - sas

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;

Related

SAS Enterprise Guide: How to query library and export results to CSV [duplicate]

This question already has answers here:
SAS proc export to CSV: how to add double quotes
(4 answers)
Closed 2 years ago.
I'm sorry if this question has already been asked but I've searched around and cannot find a clear explanation on this.
I am new to SAS, I am trying to pull out logistic recs, separate by day, and save as date_warehouse#.csv
The data is stored in a library, let's call it 'WH' and referenced table is called 'TX'
This is easy to do in terminal but the outsourced IT guy is stonewalling me about connecting via terminal so I can use PIG/HIVE, so I am being forced to go through SAS.
I import the lib using the command:
libname WH_LIB sasioimp dsn="PDB" user="username" Password="PASSWORD" schema="WH";
I am able to do a
proc sql
*query here*
quit; run;
however these values are being stored then displayed in the UI, I do see an option to then manually export to CSV.
Is there a way I can process skip the display process, and run my query in the back-end to write directly to a CSV file?
I'd like to do something like this pseudo-code in SAS:
for warehouse in warehouses:
for date in dates:
results=*run query with date and warehouse*
filename=date+warehouse+".csv"
write(results, filename)
Is there a straightforward example to accomplish this which someone could please share?
Thanks!
libname myLib ......;
%let path = C:\users\demo\;
*get list of files in your library;
proc sql;
create table list_dsn as
select memname from sashelp.vtable where libname = "MYLIB";
quit;
*function/macro to export a csv file;
%macro export_csv(dsn=);
proc export out=&dsn datafile="&path.\&dsn.csv" dbms=csv replace;run;
%mend;
*execute macro for each name in the list of files;
data _null_;
set list_dsn;
str = catt('%export_csv(dsn=', memname, ');');
call execute (str);
run;
Here's a rough approach. I would not recommend this because you know have to ensure the types for each data set is being properly read into HIVE. SAS provides several methods to use python and connect directly to the SAS data bases using python. Look into saspy and sasswat for examples on how this can be accomplished, especially if you have Viya.
You can then save this as a .sas program and call it from the command line or run it as a stored process so that it executes by default. This could be simplified into a single step but this is straightforward to follow IMO.

How to copy data afrer "cards"/"datalines" in SAS

I have to perform statistical analysis on a file with hundreds of observations and 7 variables(columns)on SAS. I know that it is necessary to insert all the observations after "cards" or "datalines". But I can't write them all obviously. How can I do? Moreover, the given data file already is .sas7bdat.
Then, since (in my case) the multiple correspondence analysis requires only six of the seven variables, does this affect what I have to write in INPUT or/and in CARDS?
You only use CARDS when you're trying to manually write a data set. If you already have a SAS data set (sas7bdat) you can usually use that directly (there are some exceptions but likely don't apply here).
First create a libname to the folder where the file is:
libname myFiles 'path to fodler with sas file';
Then load it into your work library - this is a temporary space that is cleaned up when you're done so no files here are saved permanently.
This copies it over to that library - which is often faster.
data myFileName;
set myFiles.myFileName;
run;
You can just work with the file from that library by referencing it as myFiles.myFileName in your code.
proc means data=myFiles.myFileName;
run;
This should get you started, but you should take the SAS free e-course to understand the basics, it will save you time overall.
Just tell SAS to use the dataset. INPUT statement (and CARDS/DATALINES or INFILE statement) are for reading from text files.
proc corresp data='/my directory/mydataset.sas7bdat' .... ;
...
run;
You could also make a libref that points to the directory and use two level name to reference the dataset.
libname myfiles '/my directory/';
proc corresp data=myfiles.mydataset .... ;
...
run;

How can I import this file, in SAS, without error?

I'm trying to import a file, with this code I receive the error message:
The table "WORK.HOTDOGS" cannot be opened because it does not contain
any columns.
Here is the code:
/* Import file, Print Data Table, Plot bar chart of Type vs Type Frequency */
DATA HotDogs;
INFILE '/folders/myfolders/hot_dogs.sas7bdat';
RUN;
PROC PRINT DATA=HotDogs;
RUN;
Thanks for your time.
You need to specify a library that refers to your SAS data set and/or use a SET statement. Since it looks like you're using SAS UE here's a link to their video tutorials on youtube.
https://www.youtube.com/playlist?list=PLVBcK_IpFVi9cajJtRel2uBLbtcLz-WIN
libname mydata '/folders/myfolders/';
data hotdogs;
set mydata.hot_dogs;
run;
OR
data hotdogs;
set '/folder/myfolders/hot_dogs.sas7bdat';
run;
I had the same error. It needs to be imported as follows:
data work.hotdogs;
INFILE '/folders/myfolders/hot_dogs.sas7bdat';
input make $ Model;
run;
I had the same problem in the SAS UE, solved it by deleting the dataset and re-uploading it. Somehow worked.

IGNORE DATA IN SAS IMPORT FROM EXCEL

I have no working knowledge of SAS, but I have an excel file that I need to import and work with. In the excel file there are about 100 rows (observations) and 7 columns (quantities). In some cases, a particular observation may not have any data in one column. I need to completely ignore that observation when reading my data into SAS. I'm wondering what the commands for this would be.
An obvious cheap solution would be to delete the rows in the excel file with missing data, but I want to do this with SAS commands, because I want to learn some SAS.
Thanks!
Import the data however you want, for example with the IMPORT procedure, as Stig Eide mentioned.
proc import
datafile = 'C:\...\file.xlsx'
dbms = xlsx
out = xldata
replace;
mixed = YES;
getnames = YES;
run;
Explanation:
The DBMS= option specifies how SAS will try to read the data. If your file is an Excel 2007+ file, i.e. xlsx, then you can use DBMS=XLSX as shown here. If your file is older, e.g. xls rather than xlsx, try DBMS=EXCEL.
The OUT= option names the output dataset.
If a single level name is specified, the dataset is written to the WORK library. That's the temporary library that's unique to each SAS session. It gets deleted when the session ends.
To create a permanent dataset, specify a two level name, like mylib.xldata, where mylib refers to a SAS library reference (libref) created with a LIBNAME statement.
REPLACE replaces the dataset created the first time you run this step.
MIXED=YES tells SAS that the data may be of mixed types.
GETNAMES=YES will name your SAS dataset variables based on the column names in Excel.
If I understand you correctly, you want to remove every observation in the dataset that has a missing value in any of the seven columns. There are fancier ways to do this, but I recommend a simple approach like this:
data xldata;
set xldata;
where cmiss(col1, col2, ..., col7) = 0;
run;
The CMISS function counts the number of missing values in the variables you specify at each observation, regardless of the data type. Since we're using WHERE CMISS()=0, the resulting dataset will contain only the records with no missing data for any of the seven columns.
When in doubt, try browsing the SAS online documentation. It's very thorough.
If you have "SAS/ACCESS Interface to PC Files" licensed (hint: proc setinit) you can import the Excel file with this code. The where option lets you select which rows you want to keep, in this example you will keep the rows where the column "name" is not blank:
proc import
DATAFILE="your file.xlsx"
DBMS=XLSX
OUT=resulttabel(where=(name ne ""))
REPLACE;
MIXED=YES;
QUIT;

How to create "standardized" Excel workbooks using SAS

I have a "wide" SAS data sets that must be exported into a new Excel workbook every week. I want to preserve the column widths and other Excel attributes every week, but I'm having problems getting it to work. Here's what I'm attempting.
I used PROC EXPORT to create a new workbook (using sheet="New_TACs").
I manually adjusted the column widths and other sheet attributes
(like "filters", column widths, wrap, alignment, and "freeze panes").
I deleted all the data rows (leaving the first row with the column
names) and saved it as a new workbook named "template.xlsx".
Using a SAS system call, I copy "template.xlsx" to "this_week.xlsx".
I use PROC EXPORT again to try and update the new workbook, but I
get warnings. The result contains a sheet named "New_TACS1".
Here is the SAS log:
720 proc export data=new_tacs
721 outfile="\\server-path\this_week.xlsx"
722 replace;
723 sheet='New_TACs';
724 run;
WARNING: The target file may contain unmatched range name and sheet name.
WARNING: The target file may contain unmatched range name and sheet name.
WARNING: File _IMEX_.New_TACs.DATA does not exist.
WARNING: Table _IMEX_."New_TACs" has not been dropped.
NOTE: "New_TACs" range/sheet was successfully created.
NOTE: PROCEDURE EXPORT used (Total process time):
real time 23.88 seconds
cpu time 1.80 seconds
I'm at a loss as to what to do and would appreciate any ideas or suggestions.
I think the issue is that with zero rows, SAS isn't properly dealing with the data. I can't get PROC EXPORT to work at all, but with a single dummy row I can at least get it to behave with libname and PROC APPEND. I wouldn't be surprise if the filters are in part responsible for this.
After creating the blank excel file with the SASHELP.CLASS columns, adding a filter, adding one row of dummy data, and saving/closing, I do: (SCANTEXT=NO is mandatory here for update access)
libname newtac "c:\temp\test.xlsx" scantext=no getnames=yes;
proc append base=newtac.'New_TACs$_xlnm#_FilterDatabase'n data=sashelp.class force;
run;
libname newtac clear;
That gets close, at least. I'm getting some blank rows for some reason, perhaps due to other things I did in looking at this.
Your best solution may well be to wait for 9.4 TS1M0 and ODS EXCEL, which will let you do all these things from SAS directly; or to use DDE.
I would recommend checking out SaviCells. http://www.sascommunity.org/wiki/SaviCells. It provides much better SAS to Excel functionality, including creating a template with all your Excel formatting and using that with new data.
Use DDE in SAS to achieve this.
You can create your excel template the way you want it to appear.
Using DDE you would then:
Open Excel
Open the excel file you want to use as the template
Populate it with the updated data
Save the file as a new filename
It's a bit of an antiquated technology but it gets the job done.
Googling for SAS and DDE will find you plenty of code exmaples and tutorials.