SAS, create a file name from a dataset column

Is it possible to create a file name from a value stored in a dataset column? What I need is something like:

/* other code here, assume work.users looks like user_id ImageData */ data _null_; set work.users; file_name=cat('/home/me/', trim(user_id), '.jpg'); file file_name NOPRINT; put ImageData; run; 

I'm currently trying to do this with macros, but I'm out of luck.

+4
source share
2 answers

To do this, you first need to create the file_name variable, and then you can use the filevar= option in the new data step to dynamically write to files.

So, first create the file_name in work.users :

 data work.users; length file_name $255; file_name=cats('/home/me',user_id,'.jpg'); run; 

Then do what you are trying to do using the filevar = parameter:

 data _null_; set work.users; file dummy filevar=file_name noprint; put ImageData; run; 

Note that dummy is just a placeholder when using the filevar= method.

+8
source

If you want to do this with macros:

 data _null_; set work.users; call symput('filename', cats('/home/me/', user_id, '.jpg')); run; data _null_; set work.users; file "&filename." noprint; put imagedata; run; 

However, this assumes that there is only one observation in work.users , which I assume is incorrect. If you want to display a file for each observation, collapse it into a macro:

 %macro writefile(n); %do i = 1 %to &n; data _null_; i = &i; set users point=i; call symput('filename', cats('c:\temp\', user_id, '.txt')); stop; run; data _null_; i = &i; set users point=i; file "&filename." noprint; put imagedata; stop; run; %end; %mend; 

Here, the argument & n is the number of observations in your dataset. You can get it programmatically, but for current purposes, it's easier to pass it manually to a macro.

+1
source

Source: https://habr.com/ru/post/1342227/


All Articles