SAS Format Data from "flat" data source - sas

Hope you can help.
Im trying to read flat data from mainframe with my program in SAS with this code:
INFILE DG121 TRUNCOVER
Missover FIRSTOBS=2 ;
INPUT
# 1 LAENGDE IB2.
# 3 TOTANM IB2.
# 5 REXHTYP IB2.
# 7 RMODTAFD PD3.
# 10 RANVDATO PD5.
# 15 RKUNDENR PD6.
# 21 RBRKODE PD2.
# 23 RRAADGIV ÅCHAR1.
# 24 RKUNDKAT ÅCHAR3.
# 27 RMAXOPR PD8.2
# 35 RLOBTIDO PD2.
# 37 RPRODBET ÅCHAR6.
# 43 RMEDIE ÅCHAR5.
# 48 ROPSIG ZD3.
# 69 BRUGER IB4.
I get the Note in my log:
NOTE: Invalid data for RMODTAFD in line 47291 7-9.
NOTE: Invalid data for RANVDATO in line 47291 10-14.
NOTE: Invalid data for RKUNDENR in line 47291 15-20.
NOTE: Invalid data for RBRKODE in line 47291 21-22.
NOTE: Invalid data for RMAXOPR in line 47291 27-34.
NOTE: Invalid data for RLOBTIDO in line 47291 35-36.
NOTE: Invalid data for ROPSIG in line 47291 48-50.
ERROR: Limit set by ERRORS= option reached. Further errors of this type will not be printed.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
47291 .ü.......................................................^.......................5\.................
ZONE 0A00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0500FFFFFFFFFFFFFFFFFFFF0FEFFFFFFFFFFFFFFFFF
NUMR 0100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0F0AFFFFFFFFFFFFFFFFFFFF550FFFFFFFFFFFFFFFFF
101 .......................rrrr{.r{.rr{...........¸...èhÇ..{..}.{ 161
ZONE FFFFFFFFFFFFFFFFFFFFFF0999990990999FFF00001120900058632900429
NUMR FFFFFFFFFFFFFFFFFFFFFF99999C99C999CFFF00691462D00548862C0078C
LAENGDE=161 TOTANM=0 REXHTYP=-1 RMODTAFD=. RANVDATO=. RKUNDENR=. RBRKODE=. RRAADGIV=¤ RKUNDKAT=¤¤¤ RMAXOPR=. RLOBTIDO=.
RPRODBET=¤¤¤¤¤¤ RMEDIE=¤¤¤¤¤ ROPSIG=. BRUGER=-1 _ERROR_=1 _N_=47290
NOTE: EOV macro was not able to obtain an additional extent for library data set SYS15133.T093707.RA000.G46973.R0566335, volume
number 1. System rc = 00000B37; Reason code = 00000004.
ERROR: Write to WORK.WAB.DATA failed. File is full and may be damaged.
My question is.
I know my PD format cannot read char. But can i in some way make sas skip the data it cannot read so i dont get the note:
NOTE: Invalid data for ROPSIG in line 47291 48-50

You can use ?? input format modifier to get rid of error and make sas move on with the processing. Check out Format modifier in the SAS documentation for INPUT statement ->Format Modifiers for Error Reporting.
data input_data;
infile datalines missover;
input age ?? 8.;
datalines;
12
45
32
A
54
Z
66
;;;;
run;
proc print data=input_data;run;
So your code should be like :
INPUT
# 1 LAENGDE ?? IB2.
# 3 TOTANM ?? IB2.
# 5 REXHTYP ?? IB2.
# 7 RMODTAFD ?? PD3.
.
.
.
Also, i wanted to point out that based on your log, the error that is showing up is for space out( B37 is out of space error on Mainframe). Try increasing the allocation size of the library where the dataset is getting created to prevent that error.

Related

Why does SAS skip an entire row of data values due to missing value?

When I run the following code the third observation is not output. Why does SAS omit the third observation?
data info;
input Gender $ Age Height Weight;
datalines;
M 45 72 149
F 64 62
M 61 72 271
F 29 73 125
M 16 65 178
;
Run;
title "Listing of Dataset Demographics";
proc print data=info;
run;
Defaults will get you, the default in SAS is FLOWOVER, so if a record is missing it looks for it on the next line. You want MISSOVER or TRUNCOVER instead.
Your log tells you this happened with the following note:
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
This works:
data info;
infile cards truncover;
input Gender $ Age Height Weight;
datalines;
M 45 72 149
F 64 62
M 61 72 271
F 29 73 125
M 16 65 178
;
Run;
More details are available in the Example 2 in the documentation here.
Specifically:
When you omit the MISSOVER option or use FLOWOVER (which is the default), SAS moves the
input pointer to line 2 and reads values for TEMP4 and TEMP5 (variables it cannot find). The next
time the DATA step executes, SAS reads a new line which, in this case,
is line 3. This message appears in the SAS log:
NOTE: SAS went to a new line when INPUT statement
reached past the end of a line.
Lines of text do not have "observations". They just have lines.
It didn't skip any of the lines of data. It just used two lines for the second observation because the first of the lines only had values for 3 of the 4 variables the INPUT statement requested.
This behavior is what SAS calls the flowover option of the INFILE statement. This allows you to have more than one line of text to represent the data for a single observation without having to be too persnickety about which fields you insert the line breaks between across the different observations of data.
If you don't want it to have to go hunt for the next field on the next line of text then make sure every variable has a value in the text lines. You can represent missing values by using a period for either numeric or character variables.
So use something like this:
data info;
input Gender $ Age Height Weight;
datalines;
M 45 72 149
F 64 62 .
M 61 72 271
. 29 73 125
M 16 65 178
;
When using flowover you can insert as many extra line breaks as you want as long as each new observation starts on a new line. Like this
data info;
input Gender $ Age Height Weight;
datalines;
M 45 72
149
F 64
62 .
M
61 72 271
F 29 73 125
M 16 65 178
;
If you want SAS to just give up when a there are no more values on the line use the flowover option on the infile statement.
data info;
infile datalines flowover;
input Gender $ Age Height Weight;
datalines;
M 45 72 149
F 64 62
M 61 72 271
F 29 73 125
M 16 65 178
;
There is also the older missover option, but you would normally never want that as it will set values at the end of the line that too short for an explicit INFORMAT width to missing instead of just use the number of characters that are available.
PS Don't indent lines of data. That will just make the code harder to read and the diagnostic messages about invalid data values harder to interpret. To make it easier don't intend the DATALINES (aka CARDS) statement line either. That will also make it clearer the data step definition ends where the lines of data starts and prevent you from accidentally inserting other statements for the data step after the data.

ERROR 388-185: Expecting an arithmetic operator. SAS

I am pretty new to SAS, I am trying to see which songs/artists/albums have appeared most on my spotify most played csv's (2017-2020). I am getting stuck very early on trying to just set the 2017 csv as a data set. Is there anything anyone can see that I am doing wrong? Seems like this step should be pretty straight forward.
data Spotify_2017;
infile='C:\Users\your_top_songs_2017.csv' dlm=’09’x dsd firstobs=2;
input Track URI Track Name Artist URI Artist Name Album URI Album Name Album Release Date Disc Number Track Number Track Duration Explicit Popularity Added By Added At;
run;
and here is the log:
1 The SAS System 10:18 Friday, January 15, 2021
1 ;*';*";*/;quit;run;
2 OPTIONS PAGENO=MIN;
3 %LET _CLIENTTASKLABEL='Spotify.sas';
4 %LET _CLIENTPROCESSFLOWNAME='Standalone Not In Project';
5 %LET _CLIENTPROJECTPATH='';
6 %LET _CLIENTPROJECTPATHHOST='';
7 %LET _CLIENTPROJECTNAME='';
8 %LET _SASPROGRAMFILE='C:\Users\xxx\Desktop\Spotify\Spotify.sas';
9 %LET _SASPROGRAMFILEHOST='USRDUL-PC0NNXU1';
10
11 ODS _ALL_ CLOSE;
12 OPTIONS DEV=SVG;
13 GOPTIONS XPIXELS=0 YPIXELS=0;
14 %macro HTML5AccessibleGraphSupported;
15 %if %_SAS_VERCOMP_FV(9,4,4, 0,0,0) >= 0 %then ACCESSIBLE_GRAPH;
16 %mend;
17 FILENAME EGHTML TEMP;
18 ODS HTML5(ID=EGHTML) FILE=EGHTML
19 OPTIONS(BITMAP_MODE='INLINE')
20 %HTML5AccessibleGraphSupported
21 ENCODING='utf-8'
22 STYLE=HtmlBlue
23 NOGTITLE
24 NOGFOOTNOTE
25 GPATH=&sasworklocation
26 ;
NOTE: Writing HTML5(EGHTML) Body file: EGHTML
27
28 data Spotify_2017;
29 infile='C:\Users\pcardella\Desktop\Spotify\C:\Users\xxx\Desktop\Spotify\your_top_songs_2017.csv' dlm=’09’x dsd
___
388
76
29 ! firstobs=2;
ERROR 388-185: Expecting an arithmetic operator.
ERROR 76-322: Syntax error, statement will be ignored.
30 input Track URI Track Name Artist URI Artist Name Album URI Album Name Album Release Date Disc Number Track Number Track
30 ! Duration Explicit Popularity Added By Added At;
31 run;
ERROR: No DATALINES or INFILE statement.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.SPOTIFY_2017 may be incomplete. When this step was stopped there were 0 observations and 16 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds
32
33 %LET _CLIENTTASKLABEL=;
34 %LET _CLIENTPROCESSFLOWNAME=;
35 %LET _CLIENTPROJECTPATH=;
36 %LET _CLIENTPROJECTPATHHOST=;
37 %LET _CLIENTPROJECTNAME=;
38 %LET _SASPROGRAMFILE=;
39 %LET _SASPROGRAMFILEHOST=;
2 The SAS System 10:18 Friday, January 15, 2021
40
41 ;*';*";*/;quit;run;
42 ODS _ALL_ CLOSE;
43
44
45 QUIT; RUN;
46
infile is a statement and does not need an equals sign. The syntax is:
infile 'file location here' <options>;
data Spotify_2017;
infile 'C:\Users\your_top_songs_2017.csv' dlm=’09’x dsd firstobs=2;
input Track URI Track Name Artist URI Artist Name Album URI Album Name Album Release Date Disc Number Track Number Track Duration Explicit Popularity Added By Added At;
run;
One way to help learn importing raw files using the data step is to use proc import. proc import will import the data and generate data step code for you in the log when importing csv files. You can study it to see how it works and try to replicate it.
proc import
file = 'C:\Users\your_top_songs_2017.csv'
out = spotify_2017
dbms = csv
replace;
run;
Also, a great option to help make logs more readable in Enterprise Guide is to disable autogenerated code. Go to Tools -> Options -> Results -> General -> uncheck "Show generated wrapper code in SAS log"

the error says variable X is not given, but the variable x is declared before

I get an error when trying to plot this in SAS, the error says variable X is not given, but the variable x is declared before.
Here is my code:
data prg7_5;
do a=1 to 3;
input x##;
output
end;
cards;
7028 1764 600 7228 2036 744
7228 2130 804 8448 2536 844
8567 2436 912 9061 2436 1128
9167 3108 1320 9167 3108 1464
10032 3108 1608 10051 3208 1896
;
run;
goptions hsize=5 vsize=4 ftext='宋体';
footnote 'the time ';
symbol1 interpol=boxt00 width=1.8 bwidth=5 co=red;
axis1 label =('temperature(C)')
value=('190' ' 220 ' '260')
minor=none
offset=(10,10);
axis2 label =(angle=90'the time')
offset=(0,0);
proc gplot data= prg7_5;
plot x*a/haxis=axis1
vaxis=axis2;
run;
here is the run log:
Can you show me the correct way to do it? Please help
you need a semicolon after output. Your dataset will be empty and you have following error.
ERROR 117-185: There was 1 unclosed DO block.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.PRG7_5 may be incomplete. When this step was stopped
there were 0 observations and 2 variables.
WARNING: Data set WORK.PRG7_5 was not replaced because this step was stopped.
change code with semicolon at output as shown below
do a=1 to 3;
input x##;
output;
end;

Reading the text file incorrectly in sas

Problem Statement: I have a text file and I want to read it using SAS INFILE function. But SAS is not giving me the proper output.
Text File:
Akash 18 19 20
Tejas 20 16
Shashank 16 20
Meera 18 20
The Code that I have tried:
DATA Arr;
INFILE "/folders/myfolders/personal/SAS_Array .txt" missover;
INPUT Name$ SAS DS R;
RUN;
PROC PRINT DATA=arr;
RUN;
While the result i got is :
Table of Contents
Obs Name SAS DS R
1 Akash 18 19 20
2 Tejas 20 16 .
3 Shashank16 20 .
4 Meera 18 20 .
Which is improper. So what is wrong with the code? I need to read the file in SAS with the same sequence of marks as in text file. Please help.
Expected result:
Table of Contents
Obs Name SAS DS R
1 Akash 18 19 20
2 Tejas . 20 16
3 Shashank16 20 .
4 Meera 18 . 20
Thanks in advance.
If that text file is tab-delimited, you should specify the delimiter in the infile statement and use the dsd option to account for missing values:
DATA Arr;
INFILE "/folders/myfolders/personal/SAS_Array .txt" missover dlm='09'x dsd;
INPUT Name $ SAS DS R;
RUN;
PROC PRINT DATA=arr;
RUN;
EDIT: after editing, your sample text file now looks fixed-width rather than space-delimited. In that case you should be using column input:
DATA Arr;
INFILE "/folders/myfolders/personal/SAS_Array .txt" missover;
INPUT Name $1-9 SAS 10-12 DS 13-15 R 16-18;
RUN;
example with datalines:
DATA Arr;
INFILE datalines missover;
INPUT Name $1-9 SAS 10-12 DS 13-15 R 16-18;
datalines;
Akash 18 19 20
Tejas 20 16
Shashank 16 20
Meera 18 20
RUN;

How to export SAS dataset to a .dat

I have a SAS dataset chapter5.pressure and i verified that it is fine by printing it proc print:
Obs SBPbefore SBPafter
1 120 128
2 124 131
3 130 131
4 118 127
5 140 132
6 128 125
7 140 141
8 135 137
9 126 118
10 130 132
11 126 129
12 127 135
So, I want to export it to the .dat file, and the following method does not work:
libname chapter5 'c:\users\owner\desktop\sas\chapter5';
data _null_;
set chapter5.pressure;
file 'c:\users\owner\desktop\sas\chapter5\xxx.dat';
put a b ;
run;
The resulting file has all missing values. Why
Try using the variable names instead of "a" and "b".
data _null_;
set chapter5.pressure;
file 'c:\users\owner\desktop\sas\chapter5\xxx.dat';
put SBPbefore SBPafter;
run;
Instead of using a put statement, you can also use PROC EXPORT to create a delimited file from a SAS dataset:
PROC EXPORT
DATA=chapter5.pressure
OUTFILE='c:\users\owner\desktop\sas\chapter5\xxx.dat'
DBMS=DLM
REPLACE;
RUN;
The default delimiter is a blank, which should match what you are trying to do. To create a tab or comma-delimited file, change the DBMS option value to TAB or CSV respectively. This will create a header row in the external file. Here is a link to the SAS 9.2 documentation. Check the SAS support site if you are using a different version.