DATA Step, Macro, Functions and more

remove blanks for a call symput in order to import values into memory

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

remove blanks for a call symput in order to import values into memory

[ Edited ]

I would like to apply a macro to generate different bbdd, one for each value of my_var.

 

I correclty import into memory the values with 5 digits, but I cannot remove the blank after the strings with four digits such as 9916, so I will see "9916 ", instead of "9916".

 

This cause me probles in a following code (not posted) where I want to match the values of my_var with the one of another dataset

 


data my_var;
input my_var $5.;
cards;
27016
27015
27014
27011
27013
9916
9917
9909
9908
9910
9904
9905
9911
9912
9936
9935
9937
9939
;
run;

 

data my_var; set my_var;my_var=compbl(my_var);run;

data my_var; set my_var;my_var=compress(my_var," ","");run;

data my_var; set my_var;my_var=tranwrd(my_var," ","");run;

 


* Generate List of Possible Values;
proc freq data = my_var noprint;
table my_var/ out = bbdd;
run;

 

*********************************************************************;
* Import Values Into Memory;
data _null_;
set bbdd end=last;
call symput('memory' || trim(left(_n_)), my_var);
if last then call symput('Nummemory', _n_);
run;

%put Total Number of memory Variables: &Nummemory;
%put memory Variables: &memory1
&memory2
&memory3
&memory4
&memory5
&memory6
&memory7
&memory8
&memory9
&memory10
&memory11
&memory12
&memory13
&memory14
&memory15
&memory16
&memory17
&memory18
;

 

*target is a big dataset, I want to create subset, using a "where", according to the values imported in memory

 

data target;
input my_var $5.;
cards;
27016
27015
27014
27011
27013
9916
9917
9909
9908
9910
9904
9905
9911
9912
9936
9935
9937
9939
;
run;


%let intro=p_;

option mtrace mprint;
%macro Creatememory;
%do i = 1 %to &Nummemory;
data &intro&&memory&i;
set target;where (my_var = "&&memory&i");
%end;
run;
%mend Creatememory;
%Creatememory;
*********************************************************************;

 

 

 

Thanks in advance


Accepted Solutions
Solution
‎03-31-2017 08:09 AM
Super User
Super User
Posts: 7,060

Re: remove blanks for a call symput in order to import values into memory

[ Edited ]

I cannot figure out what your question is.  Can you post example data that recreates the problem?

 

SAS character variables are fixed length and padded with spaces.  So if you create a variable of length $5 and store 'ABDC' into then the fifth position will be a blank.  When comparing variables SAS will automatically ignore the trailing spaces.

 

Note that if you want to generate macro variables that do not include the trailing spaces then use the CALL SYMPUTX() function instead of the older CALL SYMPUT() function.

138  data _null_;
139    length x $8 ;
140    x='ABCD';
141    call symput('one',x);
142    call symputx('two',x);
143  run;

NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


144  %put ONE=|&one| TWO=|&two|;
ONE=|ABCD    | TWO=|ABCD|

View solution in original post


All Replies
Solution
‎03-31-2017 08:09 AM
Super User
Super User
Posts: 7,060

Re: remove blanks for a call symput in order to import values into memory

[ Edited ]

I cannot figure out what your question is.  Can you post example data that recreates the problem?

 

SAS character variables are fixed length and padded with spaces.  So if you create a variable of length $5 and store 'ABDC' into then the fifth position will be a blank.  When comparing variables SAS will automatically ignore the trailing spaces.

 

Note that if you want to generate macro variables that do not include the trailing spaces then use the CALL SYMPUTX() function instead of the older CALL SYMPUT() function.

138  data _null_;
139    length x $8 ;
140    x='ABCD';
141    call symput('one',x);
142    call symputx('two',x);
143  run;

NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


144  %put ONE=|&one| TWO=|&two|;
ONE=|ABCD    | TWO=|ABCD|
Contributor
Posts: 27

Re: remove blanks for a call symput in order to import values into memory

I used symputx and it works, thanks!

☑ This topic is solved.

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

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