Hi,
I have a txt file with all my observation in just one record, similiar to this:
[{"Var1":"a1234","Var2":"a567","var3":0,"var4":0},{"Var1":"b1234","Var2":"b567","var3":1,"var4":0},{"Var1":"c1234","Var2":"c567","var3":0,"var4":0}, ..............................................]
I'm trying to import its content to a dataset. But even using double trailing I just end up with 2 observations (the txt file has something about 20,000 observations). This is the code:
data dataset1;
infile '/local/arquivo_1.txt' dlm='{[,:"} ;';
input var1 $ Var1_content : $9. var2 : $12. Var2_content : $12. var3 : $16. Var3_content $ var4 : $12. Var4_content $ @@;
run;
Add a LRECL option to the INFILE statement. You should be able to set it to a billion (1 with 9 zeros).
If that doesn't work then read the file using STREAM input and convert all of the '}' to '0A'x and then the line lengths will be shorter.
Add a LRECL option to the INFILE statement. You should be able to set it to a billion (1 with 9 zeros).
If that doesn't work then read the file using STREAM input and convert all of the '}' to '0A'x and then the line lengths will be shorter.
Thank you very much Tom! It worked perfectly
If this was a one-time project I would be very tempted to use and editor to replace the string },
with }<new line> where new line is the appropriate one for your OS.
data dataset1;
infile '/local/arquivo_1.txt' dlm='{[,:"} ;';
length varn varn_conts $20 ;
Do case = 1 by 1 ;
input varn Varn_conts @ ;
If varn = " " then input ;
Else output ;
End ;
stop ;
run;
Is the original file JSON?
It is JSON file which is a stream file , You need to tell SAS it is a stream file by defining recfm=n ;
data dataset1;
infile '/local/arquivo_1.txt' dlm='{[,:"} ;' recfm=n ;
input var1 $ Var1_content : $9. var2 : $12. Var2_content : $12. var3 : $16. Var3_content $ var4 : $12. Var4_content $ @@;
run;
Xia Keshan
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.