1

I have currently a dataset which contains the variables I need together with the needed formats.

Now I am using the getvarc() function (among others) in a loop to get those variables to write to a file.
The first problem occuring ofcourse, is that some variables are not char type but num type. I could use getvarn() to retrieve those, but then the format goes to waste which I really need.

E.g. date in num type: 18750. Format = yymmdd10. Thus looking like 2011-05-03

Using the value=getvarn() for this field, it would retrieve 18750 for value. If I then want to output (using PUT) this value, it would not give me the 2011-05-03 date as I want.

So now I am looking for a better way to do this.
My first option is to use the sashelp.vcolumn data set, to retrieve the format on this field, and use it in the put statement to output in the right format.
My second option is to convert the data set containing this field (in reality it is about multiple data sets), where I convert all num type fields to char types remaining the right format.

Which option should I go with?
And in case of the second option, can this be done in a generic way (as I said, this is not about one variable only) ?

EDIT:

After the answer of Cmjohn I got to find a way to fix my problem. Not using the sashelp.vcolumn but using the varfmt function. So a short explanation of what I am doing in my code:

  data _null_
  set dataset1;
  file file1;
  if somefield = x then do;
    dsid = open(datasetfield, i);
    rc = fetchobs (dsid);
    varnum = varnum(dsid,anotherfield);
    **varfmt = varfmt(dsid,anotherfield);
    if vartype(dsid,anotherfield) = 'N' then do;**
      value = getvarn(dsid,varnum);
      **value_formatted = putn(value,varfmt);**
    **end;
    else do;**
      value = getvarc(dsid,varnum);
      **value_formatted = putc(value,varfmt);
    end;**
    put value_formatted;
  end;
  run;

So this is in general (quickly out of my head) what I am doing now. The bold part of the solution is what I came up with after Cmjohns response. So my first question is answered, 'How to do it?'.

But my added question: What would be most efficient in the long run: keep this process or make my data sets the way that all data can be read in by only using getvarc without the need of the type-check and the getfmt()?

Yoh
  • 678
  • 1
  • 10
  • 18

1 Answers1

2

I'd suggest that you use the vvalue function, or any of the other techniques provided in the answers to this question to put formatted values to a file.

Community
  • 1
  • 1
cmjohns
  • 4,465
  • 17
  • 21
  • Thank you for the link to the other question. Strange I didn't find that one while searching for similar questions. But I think the `vvalue` function won't work for me, as it needs the variable name as an argument while I am using the `getvarc()` function to retrieve the argument value from another table. I am trying the `varfmt()` now, but then I still need to find a way to distinguish between char types and num types... – Yoh May 04 '11 at 17:58
  • Ok, in addition to previous comment. I now tried using varfmt(). I also needed vartype() to select the type , then using getvarc or getvarn depending on vartype and in the end using putn and putc to put it into variable again. It works, but I still wonder if it would be better to do this beforehand (like my second option). – Yoh May 04 '11 at 18:59
  • 1
    @Yohsoog - From the question, it looked liked you were just trying to convert a data set to a text file, while preserving formatting. I'm not sure now that I understand fully what you are trying to do. Perhaps if you provided a small example - along with the expected result - we could come up with something more useful. – cmjohns May 04 '11 at 20:09
  • I just edited my question to reflect these changes. By doing so, I noticed my question indeed was not very strong. I tried to rephrase it now, but if it is still not clear, I will accept your answer and try to open another question for the efficiency part. – Yoh May 04 '11 at 22:35