1

Could anyone tell me what this data step is doing? I have never seen the use of "!!" or a "double exclamation mark" before?

data _null_;
set &dset_in.;
if i = _n_ then do i=1 to nvar;
    call symput ("var" !! strip(put(_n_, 3.)), strip(Variabile));
    call symput ("min" !! strip(put(_n_, 3.)), strip(lim_inf));
    call symput ("max" !! strip(put(_n_, 3.)), strip(lim_sup));
end;
run;

A general gist of the loop would be helpful too, thanks

3 Answers3

4

!! is a concatenation operator as per the documentation:

https://documentation.sas.com/?docsetId=lrcon&docsetVersion=9.4&docsetTarget=p00iah2thp63bmn1lt20esag14lh.htm&locale=en

The code is creating a set of macro variables VAR001, MIN001 MAX001 to store values in a data set. In general this is a bad practice, if you need to do this there's usually an easier way somehow.

Reeza
  • 20,510
  • 4
  • 21
  • 38
  • I want to approve both but r.user was first –  Oct 04 '18 at 16:16
  • also, quick Q, i don't understand how this works if it says if i = _N_ before the loop? –  Oct 04 '18 at 16:16
  • What is the value of nvar? It partly depends on how the input data is set up so I'm kinda guessing here. – Reeza Oct 04 '18 at 16:19
  • unfortunately, I don't have the data but from reading this code, it's the number of variables ( this was created by a call symput earlier) –  Oct 04 '18 at 17:31
  • @MattMcilwham Please don't feel like you need to just approve an answer because it was first. You should give the checkmark to the best, or most helpful, answer in your opinion. – Joe Oct 04 '18 at 20:45
  • No, then it would be &nvar. Since there’s no ampersand it has to be a variable in the dataset. – Reeza Oct 04 '18 at 21:57
  • @joe, they were both equally good answers and on that basis, I went for the first –  Oct 05 '18 at 07:19
2

It's just used for concatenation (instand of || ).

r.user.05apr
  • 5,356
  • 3
  • 22
  • 39
2

Answering the second part of the question, what it's doing is:

There is a variable i which has some value indicating a row that the user wants to retrieve values from. When that row is reached, the values in three variables are stored in macro variables that have the row number in their name, such as if i=5, then it will take the 5th row, and put the three variables in &var005, &min005, &max005.

However, there are a number of issues with this code. First off, reusing i in that loop is a bad idea; while it doesn't do anything permanent, it easily could with minor changes that could arise from other features/bugfixes.

Second, the loop as currently structured is pointless. It's not doing anything based on i, so it's just putting the same 3 values into the same 3 macro variables multiple times. It looks to me like this is someone's take on code they copied from the internet, but slightly misunderstood.

Probably what it should do, is something like this:

data _null_;
  set &dset_in.;
  call symput ("var" !! strip(put(_n_, 3.)), strip(Variabile));
  call symput ("min" !! strip(put(_n_, 3.)), strip(lim_inf));
  call symput ("max" !! strip(put(_n_, 3.)), strip(lim_sup));    
run;

That would make macro variables for each row with the row number in the name, using the automatic data step loop. This is just a guess, though, not knowing anything about the program beyond what I see here.

But that's all with the caveat that this whole operation is a bad idea; storing data values in macro variables is poor programming practice. Don't do it.

Joe
  • 62,789
  • 6
  • 49
  • 67