DATA Step, Macro, Functions and more

Assigning a variable to macro variable

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

Assigning a variable to macro variable

 

 

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;

Accepted Solutions
Solution
‎08-24-2016 10:17 PM
Super User
Posts: 10,497

Re: Assigning a variable to macro variable

[ Edited ]

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


All Replies
Solution
‎08-24-2016 10:17 PM
Super User
Posts: 10,497

Re: Assigning a variable to macro variable

[ Edited ]

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"

 

 

Contributor
Posts: 55

Re: Assigning a variable to macro variable

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 329 views
  • 0 likes
  • 2 in conversation