BookmarkSubscribeRSS Feed
Jumboshrimps
Obsidian | Level 7

Have 60 or so files in UNIX server, some are csv, some are txt, some are xlsx.                                                                                             

Vendors have agreed to place date of file the last eight characters of file name, just before extension.   These files cannot be opened, modified or changed in any way as they are not on our servers.   Code below extracts the eight characters correctly.                                                                                                                                                  

data filenames;
length fref $8 f_name $52;
did = filename(fref,'/big_long_path_name/');
did = dopen(fref);
do i = 1 to dnum(did);
f_name = dread(did,i);
output;
end;
did = dclose(did);
did = filename(fref);
keep f_name;
run;

 

/*Now need that string to be saved as a variable so I can append it do the final SAS .sas7bdat data sets such as                                     "big_gnarly_data_10_31_22.sas7bdat"

 

data null;
length dstring $8.;
set filenames;
dstring = substr(F_name,length(F_name)-12,8);
%put &dstring.;
run;

Dstring is is in fact a variable with the correct date of the file in the Unix directory.                                                                                 

 

But %put &dstring. literally displays "substr(F_name,length(F_name)-12,8)".                                                                                         

This is not helpful, as I need to append this variable again and again for each final data set.

 

THX.

3 REPLIES 3
sbxkoenk
SAS Super FREQ

Hello,

 

There is no macro variable &dstring. !

Replace 

%put &dstring.;

by

put dstring=;

 

See your LOG for the dstring-s you are after.

 

Koen

Tom
Super User Tom
Super User

The code should not display anything for the macro variable DSTRING because there is no place where that macro variable was ever assigned a value.

 

If you want to display the value of the variable DSTRING then use the PUT statement, not the macro %PUT statement.

data null;
  length dstring $8.;
  set filenames;
  dstring = substr(F_name,length(F_name)-12,8);
  put dstring = ;
run;

Also remember that the macro processor finishes pre-processing the code before it is passed onto SAS to evaluate.  So your last step should have been written in this order to properly indicate the order that the statements will be evaluated.

%put &dstring.;
data null;
  length dstring $8.;
  set filenames;
  dstring = substr(F_name,length(F_name)-12,8);
run;

If you really need to create a macro variable from the values in the dataset FILENAMES then you can use the CALL SYMPUTX() function.

data null;
  length dstring $8.;
  set filenames;
  dstring = substr(F_name,length(F_name)-12,8);
  call symputx('dstring',dstring);
run;

But if there is more than one observation in the dataset FILENAMES then the value of the macro variable DSTRING will be just the value from the LAST observation in the dataset.  The previous values assigned to the DSTRING macro variable will have been replaced.  But since you created the DSTRING variable in the dataset NULL (since you did not use the _NULL_ keyword in the DATA statement that would tell SAS you did not want to make a dataset) you can see all of the value by using that dataset.

proc print data=null;
run;
Kurt_Bremser
Super User

%PUT is a macro statement. The macro processor is a preprocessor which does its work while code is compiled, long before the code is actually executed.

Macro code can therefore also not use data step variables, as these come into exustence long after the macro processor did its work.

What you see from your %PUT is a remnant from an earlier try where you used %LET. Your code does not set the macro variable &dstring anywhere, so on its own it would result in a log message telling you that an apparent symbolic reference could not be resolved.

 

To store values from a data step into a macro variable, you need the CALL SYMPUTX routine. To display the newly created macro variable, the %PUT must be entered after the RUN which terminates the data step.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 281 views
  • 0 likes
  • 4 in conversation