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

Hello!

I am trying to create a marco variable x, that countains numbers. However there are always leading blanks. As i want to use the macro variable later in a table name, it must not contain blanks. I found a solution to this Problem using %sysfunc. However, I am wondering: Is there an "easier" war to do that?

Thank you,

S.

---------------------------------------------------------

/* kleinste Primzahl bestimmen */

            title 'Primzahl';

            proc sql;

                  select min(primzahl) INTO x

                  from temp;

           

                  /* Primzahl aus der Liste entfernen */

                  delete from temp

                  where primzahl=&x;

            quit;

      /* kleinste Primzahl formatieren */

      %LET X = %sysfunc(cat(&x));

      %put Primzahl = &x;

/* Quadrate mod x bestimmen */

      data tempi;

            do Basis = 1 to &x;

                  Quadrat_mod_&x = mod(Basis ** 2, &x);

                  output;

            end;

      run;

1 ACCEPTED SOLUTION

Accepted Solutions
Keith
Obsidian | Level 7

One way is simply to add the CAT function to the SQL statement

     select cat(min(primzahl))

View solution in original post

7 REPLIES 7
Keith
Obsidian | Level 7

One way is simply to add the CAT function to the SQL statement

     select cat(min(primzahl))

Haikuo
Onyx | Level 15

Thanks, Keith. That is a really neat way to use cat() or cats().

If going for the 'more' text-book style approach, OP can also try:

select left(put(min(primzahl),8.))

Regards,

Haikuo

sfmeier
Obsidian | Level 7

Hello Hai.kou,

thank you very much for your help! is there any advantage to your code except for its textbookness? 😉

Regards, Stefan

Haikuo
Onyx | Level 15

I doubt that, as my approach involves one more function than Keith's. That being said, if you want to know the truth difference in term of efficiency, you probably need to benchmark it.

Regards,

Haikuo

sfmeier
Obsidian | Level 7

Thank you, Keith! it works 🙂

Ksharp
Super User

select put(min(primzahl),32.-L))


or


%LET X = %left(&x);



ScottBass
Rhodochrosite | Level 12

A "trick" I learned a while back (via SAS-L) is to use "separated by".  For a single numeric return value, it will strip leading and trailing blanks.

data one;

  x=1;

run;

proc sql noprint;

  select x into :mvar separated by " " from one;

quit;

%put *&mvar*;

HTH,

Scott


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

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
  • 7 replies
  • 3390 views
  • 7 likes
  • 5 in conversation