BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jwillis
Quartz | Level 8

Jaap,

I am commenting to learn. I tested the agoldma code.  The formats 3. and PD3. work fine all by themselves.  If I understand the link you provided,

http://support.sas.com/documentation/cdl/en/leforinforref/63324/HTML/default/viewer.htm#n0xnvrbp96w3..., data written as .pd4 should be read as .pd4.  I wrote my data as .pd3 and read it in as .pd3 and the process worked as expected. When I wrote .pd3 and read as .pd2, the file read in the number "0" not the number "-12". 

I am thinking ahead and imagining two files, one created with .pd4 and the other created with .pd3.  How would I build code that would adjust itself to change the format to .pd3 or .pd4 based on the file being read.  My solution is to use macro variables.  I believe that agoldma is trying to build a proc format solution. Data _nulls_ solution is very novel to me.  I have never seen  "invalue inpdthree default=3 max=3 other = [pd3.] ;"  and I have also never seen brackets "[ ]" used in a Proc format.

Thank you to all who post Questions and Answers.  Your thoughts and Ideas are priceless and definitely cheaper than taking SAS courses.

37

38 %let thre=3.;

39 %let pdthre=pd3.;

40 data _null_;

41 x=-12;

42 file 'E:\test5.txt';

43 put @1 x &thre.

44 @4 '*'

45 @5 x &pdthre.

46 @8 '#'

47 ;

48 run;

NOTE: The file 'E:\test5.txt' is:

Filename=E:\test5.txt,

RECFM=V,LRECL=256,File Size (bytes)=0,

Last Modified=25Jun2014:07:33:06,

Create Time=24Jun2014:08:10:31

NOTE: 1 record was written to the file 'E:\test5.txt'.

The minimum record length was 8.

The maximum record length was 8.

 

49

50 proc sql;

50 ! drop table work.test5;

NOTE: Table WORK.TEST5 has been dropped.

50 ! quit;

 

51 data test5;

52 infile 'E:test5.txt' truncover;

53 input @1 x13 &thre.

54 @4 sep1 $1.

55 @5 x1pd &pdthre.

56 @8 sep2 $1

57 ;

58 run;

NOTE: The infile 'E:test5.txt' is:

Filename=E:\test5.txt,

RECFM=V,LRECL=256,File Size (bytes)=10,

Last Modified=25Jun2014:07:33:06,

Create Time=24Jun2014:08:10:31

NOTE: 1 record was read from the infile 'E:test5.txt'.

The minimum record length was 8.

The maximum record length was 8.

NOTE: The data set WORK.TEST5 has 1 observations and 4 variables.

 

59

60 proc print data=test5; run;

Obs    x13    sep1    x1pd    sep2

    1     -12     *             -12      -

agoldma
Pyrite | Level 9

Jwillis,

Your example (in post # 15) shows one example where an informat could be useful -- giving the user of the program a choice of which informat to use.

My situation is different. I have inconsistent data in a text file (with some compressed fields). In the same location on different records I could have things like: '800012'x, '000000'x, '202020'x, '      ', '123', 'TOM' ... all kinds of stuff.

Without using my own user-defined informat, I could still read this data. I would pause my INPUT line with @; use an IF statement to check the contents of the input buffer, and then read it appropriately. However, I think that using my own informat is at least more elegant if not faster -- to do the same kind of conditional reading.

agoldma
Pyrite | Level 9

Jaap, thank you for suggesting the use PROC FCMP

It seems to be similar to a macro, but apparently you can use it inside an informat. It can do some pre-processing while reading data from a text file... very interesting, and.... that example #12 in Proc Format "Creating a Function to Use as a Format" shows the use the (DEFAULT=) option with parentheses.

Jwillis, if you like macros, you might like PROC FCMP better. Looks like it allows you to do everything that a macro does but with easier syntax.

jwillis
Quartz | Level 8

Agoldma and Data_Null_,

Is your code to exclude parentheses from "Default= Max=" intentional?  When I leave off the parentheses in the invalue statement, it appears that the default= and max= statements are part of the 'start' and 'end' definitions of the format. When I put the parentheses around

the default and max words, the format looks like I expect it to.

invalue inpdthree (default=3 max=3) other = [pd3.]


options nofmterr;
proc format; invalue inthree   default=3 max=3
              other=3. ;
             invalue inpdthree default=3 max=3
              other=pd3. ; 
run;

proc format; value inthree   (default=3 max=3)
              other=3. ;
             value inpdthree (default=3 max=3)
              other=pd3. ; 
run;


proc format fmtlib;
select @inthree inthree @inpdthree inpdthree ;
run;

data_null__
Jade | Level 19

Yes you are correct this omission of parenthesis around the length options is the issue.  I did not even notice that yesterday.  Correcting the syntax of the INVALUE fixes "everything".

agoldma
Pyrite | Level 9

Data_null_, I'm sorry for unmarking your previous responses as correct and helpful, but the real problem was in the parentheses. Jwillis had it right.

I knew I had to use the default= option because SAS Help said so, but I didn't use it correctly, and unfortunately SAS didn't give me an error.

I'm also interested in proc fcmp. Thank you, jaap, for suggesting it. I'll try to make it work later.

data_null__
Jade | Level 19

agoldma wrote:

Data_null_, I'm sorry for unmarking your previous responses as correct and helpful, but the real problem was in the parentheses.

No problem.  We want the correct correct answer flagged.Smiley Happy

agoldma
Pyrite | Level 9

Jwillis,

I'm responding your post # 16

The use of brackets [] in PROC FORMAT is documented in SAS Help: FORMAT Procedure > INVALUE Statement as "[existing-informat]"

... right next to the place that says to use parentheses around the "DEFAULT= option" with an existing-informat, so I knew I had to use it, but I missed the parentheses (max= option is not necessary; I used it because the default= option didn't work)

You're absolutely right that without the parentheses, SAS interprets "default=3 max=3" as 2 more value-range-sets. That's why it wasn't giving me an error.

jwillis
Quartz | Level 8

Agoldma,

Please post your solution.  When I tried your solution with Data_Null_s enhancement, I do not achieve the answer your want.  Thank you!

agoldma
Pyrite | Level 9

Jwillis, I'll try to look at your code later today (that's a lot of code, glad to see that you're interested in dinasaurs)

If you start with my original code and change 2 things that data_null_ suggested, the user-defined-informat-method should work

The 2 things are:

use "other" instead of "low-high" in the definition of the informat

use "truncover" on the input line

Also... I think you're right about using parentheses for the options (deault=3 max=3)

I'll try to look at it later today

jakarman
Barite | Level 11

Using the trunc over is indicating you are probably having a working approach in this case but really correct it is not.

Working with PD packed types only uneven lengths makes sense as you have to add one for the nibble sign and divide by two to get the length in bytes. Only bytes are the smallest addressable unit. Even at 64bit machine work in in full words of that size. Just accept the difference of nibbles and bytes is the most toughest thing.

For dinosaurus the last nibble in the big endian mainframe is the sign as in the little endian world orders are more logical in the other way (intel) it can be the first one there.

---->-- ja karman --<-----

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 25 replies
  • 1546 views
  • 0 likes
  • 5 in conversation