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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

2 REPLIES 2
Tom
Super User Tom
Super User

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|
progster
Fluorite | Level 6

I used symputx and it works, thanks!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 3931 views
  • 0 likes
  • 2 in conversation