New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
helannivas88
Obsidian | Level 7

Hi,

 

Im trying to create a test file with today date in Linux from SAS program using the below query , but I'm not able to result.

 

X "bhosts | sed 's/ /,/g' | sed 's/,\{2,\}/,/g' > /home/test_"$(date +%%F)".txt";

 

But I when I run directly in Linux the below query  its working.

bhosts | sed 's/ /,/g' |  sed 's/,\{2,\}/,/g' > /home/test_"$(date +%F)".txt

 

Whether variable will not able to be passed in X command???

 

Thanks.

1 ACCEPTED SOLUTION
5 REPLIES 5
Tom
Super User Tom
Super User

Change the X command to the %PUT statement so you can try to see what you are trying to pass to the X command.

704   %put "bhosts | sed 's/ /,/g' | sed 's/,\{2,\}/,/g' > /home/test_"$(date +%%F)".txt";
"bhosts | sed 's/ /,/g' | sed 's/,\{2,\}/,/g' > /home/test_"$(date +%%F)".txt"

So you have given the X command a quoted string that starts with bhosts and ends with test_, then the unquoted characters $(date +%%F) and then the quoted string .txt.  Note that %F looks like a macro call.  Do you actually have a macro named F to call?

 

Would be much better to wrap the command inside of single quotes to prevent the macro processor from trying to interpret the & and % characters.  Since your command already had single quotes in it you need to double them. 

x 'bhosts | sed ''s/ /,/g'' | sed ''s/,\{2,\}/,/g'' > /home/test_"$(date +%F)".txt' ;

Or convert them to double quotes instead.

x 'bhosts | sed "s/ /,/g" | sed "s/,\{2,\}/,/g" > /home/test_"$(date +%F)".txt' ;

Looks like you are running the bhosts command and piping the results to a file.  Why not just use a data step instead?

data _null_;
  infile 'bhosts' pipe;
  file "/home/test/%sysfunc(date(),yymmdd10).txt";
  input;
  _infile_=tranwrd(translate(_infile_,',',' '),'{2,}',',');
  put _infile_;
run;

 

 

Kurt_Bremser
Super User

+%F is a format qualifier that directs date to output a full format (YYYY-MM-DD).

 

I think that the double quotes around the date call are a mistake, as are the double percent signs. When running this command from SAS, I'd let SAS compute the date.

helannivas88
Obsidian | Level 7

Thanks @Kurt_Bremser .It was successful with your query. .

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1189 views
  • 0 likes
  • 3 in conversation