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

 

 

I just want to assign a file path to FILENAME in the below code but I know I am doing some simple mistake to get the result.

 

 

Actual issue: Text2 variable value(Contains file path with name) need to assigned to macro variable, so that I can assign macro variable for FILENAME function for file path. But macro variable is not working in Filename function.

 

 

    data testData;
      set ds2;
      text=cats(put(date,yymmn6.),put("W",1.),put(week,1.));
      text1=cats(put("Final_file_",12.),put(text,10.),put(".txt",5.));
      text2 = cats(put("""",1.),put("/path/Data_process/",50.),put("/",1.),put(text1,25.),put("""",1.));
      call symputx('nm_fl',put(text2,50.),'G');
    run;
    --------------------------------------------------------------
        %put &nm_fl;
        filename myfile &nm_fl1.;
    -----------------------------------------------------------------
        data "/pathsample_pipe_delimited_dataset";
      	infile myfile dlm='|' dsd FIRSTOBS=2;
      	input FIELD1 $ FIELD2;
      	format FIELD2; 30.;
    	run;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

There is absolutely no need to include put and a format in your CAT function calls when a variable is not involved. Make sure that the formats match the variable type or unexpected results may occur.

You should show the log and the likely errors generated:

 text1=cats(put("Final_file_",12.),put(text,10.),pu​t(".txt",5.));

Since "final_file_" is literal text Put using a 12. format is wrong as 12. is for numeric

 

      text=cats(put(date,yymmn6.),"W",put(week,1​.));
      text1=cats("Final_file_",text,".txt");
      text2 = cats('"/path/Data_process/',text1,'"');
      call symputx('nm_fl',text2,'G');

may be a bit closer to what you want. And did you really want 2 / next to each other in text2?

 

Generally I avoid actually placing quotes inside of macro variables as it seems the results are more fragile.

Did you try %Put &nm_fl to see what the variable resolved to?

Also, is there more than one record in your dataset DS2? If so, you are only going to get one macro variable for the last record as a result.

 

Also note that

data "/pathsample_pipe_delimited_dataset";

is possibly going to have an issue with the length of the name besides the poor idea of the "name literal"

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

There is absolutely no need to include put and a format in your CAT function calls when a variable is not involved. Make sure that the formats match the variable type or unexpected results may occur.

You should show the log and the likely errors generated:

 text1=cats(put("Final_file_",12.),put(text,10.),pu​t(".txt",5.));

Since "final_file_" is literal text Put using a 12. format is wrong as 12. is for numeric

 

      text=cats(put(date,yymmn6.),"W",put(week,1​.));
      text1=cats("Final_file_",text,".txt");
      text2 = cats('"/path/Data_process/',text1,'"');
      call symputx('nm_fl',text2,'G');

may be a bit closer to what you want. And did you really want 2 / next to each other in text2?

 

Generally I avoid actually placing quotes inside of macro variables as it seems the results are more fragile.

Did you try %Put &nm_fl to see what the variable resolved to?

Also, is there more than one record in your dataset DS2? If so, you are only going to get one macro variable for the last record as a result.

 

Also note that

data "/pathsample_pipe_delimited_dataset";

is possibly going to have an issue with the length of the name besides the poor idea of the "name literal"

 

 

jayakumarmm
Quartz | Level 8

Thanks a lot for your solution. It works great now.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 1550 views
  • 0 likes
  • 2 in conversation