Error reading fixed-formats in SAS - input

Here are a few lines of data.
q 2016 55 59 580067.12 89453.03 74579.31 63005.34 54211.66
q 2016 60 64 826983.94119020.88 99145.49 85347.23 75223.34
q 2016 65 69 1080400.00139847.91116260.10103226.14 93063.24
q 2016 70 74 1086917.25120158.78100291.15 91782.05 85081.34
I saved that in a file called "junk.txt". The follow bits of SAS code behave differently on those lines.
filename junk "junk.txt";
data temp;
infile junk ;
input
#1 TYPE $ 2.
#4 year 4.
#9 age1 2.
#12 age2 2.
#15 foo 10.2
#25 bar 9.2
#34 YSD1-YSD3 9.2;
run;
proc print;
data temp;
infile junk ;
input
#1 TYPE $ 2.
#4 year 4.
#9 age1 2.
#12 age2 2.
#15 foo 10.2
#25 bar 9.2
#34 YSD1 9.2
#43 YSD2 9.2
#52 YSD3 9.2;
run;
proc print;
I get an erroneous read from the first input, and a correct read from the second input. Trying to figure out what is going on. I actually have a lot more variables than 3, so being able to use the shortcut syntax would be useful to me.

A colleague of mine was familiar with syntax I was not familiar with. The behavior of the following is the same as the behavior of my second technique.
filename junk "junk.txt";
data temp;
infile junk ;
input
#1 TYPE $ 2.
#4 year 4.
#9 age1 2.
#12 age2 2.
#15 foo 10.2
#25 bar 9.2
#34 (YSD1-YSD3) (9.2);
run;
proc print;

Related

different variables with the same name in a sas dataset

I have a text file where two different columns are having the same name. As shown in the following figure.
Let's say for SystBP, I need to change the first SystBP to SystBP_B and the second SystBP to SystBP_E.
Could someone kindly offer me some help on this?
When programming in SAS Base You should sometimes not expect SAS to read column names from a text file and interpret them as variable names.
You have to instruct SAS what the first data row is, where the values are written and how they should be interpreted (text, number, date, ...) You do that with an infile and an input statement in a data step.
As you write the code yourself, you have complete control.
data READ_FROM_TXT;
infile "C:\myFolder\myFile.txt" firstobs=3 truncover;
* firstobs=3 makes SAS skip the first 2 observations;
* truncover avoids jumping to the next line when the last variable is missing or too short ;
input
#01 ID 2.
#05 Week 4.
#11 SystBP_B 6.
#19 DiastBP_B 6.
...
#41 SystBP_E 6.
#49 DiastBP_E 6.
...
;
* #11 SystBP_B 6. instructs SAS to interpret positions 11 to 16 as a number
* and assign the value to variable SystBP_B;
run;
As you inserted the data as an image, not as text, using markup, I had to guess the positions, so you will have to correct them.
I would make timing into observations.
data test;
infile cards4 firstobs=2;
input id :$8. week #;
do time = 'STR','END';
input SystBP DiastBP Pulse Stress #;
output;
end;
cards;
ID Week SystBP DiastBP Pulse Stress SystBP DiastBP Pulse Stress
1 1 134 44 66 5.8 134 44 66 5.8
;;;;
run;
The INFILE option FIRSTOBS= will let you INPUT the data starting in row 3.
Data file: C:\temp\bp-survey.txt
Start End
ID Week SystBP DiastBP Pulse Stress SystBP DiastBP Pulse Stress
1 1 134 44 66 5.8 134 44 66 5.8
...
Program
filename survey 'c:\temp\bp-survey.txt';
data want;
infile survey firstobs=3;
input
ID Week
SystBP_start DiastBP_start Pulse_start Stress_start
SystBP_end DiastBP_end Pulse_end Stress_end
;
run;
ods html ;
proc print data=want;
run;

SAS - Conditional input statement

I would like to use conditional if...then...else to read in the following data set, to read in using one input statement if source =1 and to read in using another input statement if source = 2. Not sure where my error is. This is what I have so far and the associated error. Not sure if the pointers are needed.
DATA results2;
infile datalines missover;
input #10 source 1. #;
if source = 1 then input #1 id #4 name $ #12 score;
else if source = 2 then input #1 id #4 score #12 name $;
DATALINES;
11 john 1 77
11 88 2 james
22 bobby 1 55
22 89 2 opey
;;;;
RUN;
It is correctly reading in the id but the source is not correctly matched to the id and having an issue with the name and score.
Thanks for helping!

dynamic import for file using wildmatch

dataimport:
LOAD
#1 AS CoCd,
#2 AS Period,
#3 AS [Doc. Date],
#4 AS [Pstng Date],
#6 AS Reference,
#7 AS DocumentNo,
#8 AS Crcy,
#9 AS Year,
#10 AS [Doc. Type]
FROM
\\cagta5454\Indirect\Clients\\zz Work-in-Progress\2014\data\*_110_*GLDetl*
;
I want help in dynamic import for a file
currently file is located at
\\cagta5454\Indirect\Clients\\zz Work-in-Progress\2014\data\*_110_*GLDetl*
I am looking for a way so that I can able to do dynamic import
something like creating a variable
$dataLocation = \\cagta5454\Indirect\Clients\\zz Work-in-Progress\2014\data\*_
and $datafiles = '110','121','141'
so that instead of using number for the file containing data , I can use a variable
The for each ... next function can work for you. For more examples you can always make reference to the QV Help file c:\Program Files\QlikView\English.chm or the online version of it at https://help.qlik.com/en-US/#
let dataFiles = '110, 121, 141';
for each i in $(datafiles)
let dataLocation = '\\cagta5454\Indirect\Clients\\zz Work-in-Progress\2014\data\*_' & '$(i)' & '_*GLDetl*';
dataimport:
Load
#1 AS CoCd,
#2 AS Period,
#3 AS [Doc. Date],
#4 AS [Pstng Date],
#6 AS Reference,
#7 AS DocumentNo,
#8 AS Crcy,
#9 AS Year,
#10 AS [Doc. Type]
From
$(dataLocation)
;
next

Editting multiple rows in Pandas according to its value

I have a column which goes in a pattern like this:
RS
RS
GF
NB
BP
TO
RS
GF
NB
BP
TO
...
and I want to convert the RSs into RS1 and RS2. The first one should be RS1 and the second one should be RS2. And the one in the middle needs to be RS1. And this pattern repeats on. How would I do this in pandas?
Assuming you have a DataFrame column which repeats every 11 rows
df['col']
# col
#0 RS
#1 RS
#2 GF
#3 NB
#4 BP
#5 TO
#6 RS
#7 GF
#8 NB
#9 BP
#10 TO
#11 RS
#12 RS
# ...
then you can use simple slicing
df.ix[ df.index[0::11],'col'] = 'RS1'
df.ix[ df.index[1::11],'col'] = 'RS2'
df.ix[ df.index[6::11],'col'] = 'RS1'

loop through array to load data in qlikview

I am loding data in qlikview using statement
/ /Importing data from flat file
dataimport:
LOAD #1 AS CoCd,
#2 AS Period,
#3 AS [Doc. Date],
#4 AS [Pstng Date],
#5 AS TranslDate,
#6 AS Reference,
#7 AS DocumentNo,
#8 AS Crcy,
#9 AS Year,
#10 AS [Doc. Type],
\\cagesre005\*GLDetl*
(txt, codepage is 1252, no labels, delimiter is ';', msq)
where #10 = 'KA' or #10 = 'KG' or #10 = 'KR' or #10 = 'KH' or #10 = 'KN' or #10 ='AB' or #10 ='IK' or #10 ='IM' or #10 ='MM' or #10 ='RE' or #10 ='RN';
this statement loads data perfectly but it is not dynamic since if I want to change the #10 to some different value I have to make change directly to the script, I am looking for a way that loop through a array containing these values and load data to the table
something like creating variable
$(vDocTypes) = 'KA','KG','KR','KH','KN','AB','IK','IM','MM','RE' ,'RN';
which I can use in where clause that goes through the values in the array and load the data
You can always use the Match function:
set vDocTypes= 'KA','KG','KR','KH','KN','AB','IK','IM','MM','RE' ,'RN';
//Importing data from flat file
dataimport:
LOAD
#1 AS CoCd,
#2 AS Period,
#3 AS [Doc. Date],
#4 AS [Pstng Date],
#5 AS TranslDate,
#6 AS Reference,
#7 AS DocumentNo,
#8 AS Crcy,
#9 AS Year,
#10 AS [Doc. Type]
From
\\cagesre005\*GLDetl* (txt, codepage is 1252, no labels, delimiter is ';', msq)
Where
Match( #10, $(vDocTypes) ) > 0
;