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()
?