BookmarkSubscribeRSS Feed
wzy1993
Calcite | Level 5
Thanks. But what do you mean "Post it at IML forum"? I never post a IML question, this is the first time I asked it. Can you tell me what do you mean in terms of solving this problem? Thanks.
11 REPLIES 11
wzy1993
Calcite | Level 5
Here is the code that I used trying to replace 99 and -99 with period "." using "do" loops (we are not allowed to use array in this exercise). However, the code I did only generates the replacing results in the result window, but 99 and -99 won't be replaced in the original data in the library I created. I have tried many times, but I don't why know the period won't replace 99 and -99 in the original data. I attached my code file here well. Please let me know if anyone can answer my questions.  I attended my code as "hw2" and the data file as "exercise2" below.    Thanks!!!!
My code for the 1st and 2nd question (Only the code for 2nd question has issues): 
 
libname HW2 'C:\Users\Administrator\Desktop\BU_2019\BS 803\HW\HW2';
/*1.Read only the following variables into a matrix called CG: NACZZMS, NACCLMI, NACCZLMD,
NACCDFT, NACCAGEB.*/
proc iml;
/* read variables from a SAS data set into a matrix */
  varNames = {"NACCZMMS" "NACCZLMI" "NACCZLMD" "NACCZDFT" "NACCAGEB"};
use hw2.exercise2; /* open data for reading     */
read all var varNames into CG;  /* create matrix with 5 cols */
close hw2.exercise2;
print CG[c=varNames];  
proc iml;
/* read variables from a SAS data set into a matrix */
  varNames = {"NACCZMMS" "NACCZLMI" "NACCZLMD" "NACCZDFT"};
use hw2.exercise2; /* open data for reading     */
read all var varNames into CG;  /* create matrix with 5 cols */
do j=1 to 4;
  do i=1 to nrow(CG);
     if CG[i,j]=-99 | CG[i,j]=99 then CG[i,j]=.;
  end;
end;
print CG[c=varNames];
quit;
Patrick
Opal | Level 21

I don't know enough about Proc IML to tell if it allows you to replace a source table. 

You've nicely commented your code and it doesn't say anywhere "write" or "update" - which is what's missing.

Here a link how such a write could look like:

https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html 

 

And here docu sample code how to update data:

http://support.sas.com/documentation/cdl/en/imlug/67502/HTML/default/viewer.htm#imlug_worksasdataset... 

wzy1993
Calcite | Level 5
Thanks. The "print CG[c=varNames];" before "quit" gives me the replaced results, but only in SAS Result window. However, the original data in the library I created still is the same, which means missing values 99 and -99 still exist. That is the issue.
Ksharp
Super User

Post it at IML forum. I remembered there is already a post like yours in IML forum.

Aren't you  the same person ?

k93943
Calcite | Level 5
 
PaigeMiller
Diamond | Level 26

I don't have a matrix to test this on, but I think this ought to work

 

where = loc(m=-99 or m=99);
m[where]=.;

No loops needed (in general, you don't want to use loops in IML anyway, you want to use matrix commands; the only time you would use loops is if you are developing some sort of iterative algorithm) 

--
Paige Miller
ballardw
Super User

If you receive values that you know are going to be set to missing, such as with an indicator code like this, you might consider reading the data into missing to begin with.

proc format library=work;
invalue myvalues
'99', '-99' = .
;
run;
/* reading raw data*/
data example;
   input v1 :myvalues. ;
datalines;
1 
2
9
99
-99
-45
45
;

Another option would be use an assignment of special missing in the format like .Z (replace the . in the invalue with .Z )that would allow you to tell the difference between missing because no value was read or the missing set due to this rule. The value is still missing for purposes of calculations but can be printed or examined.

Ksharp
Super User

Why not post it at IML forum ? since it is IML question ? @Rick_SAS  is there.

 

data example;
   input v1 ;
   v2=v1;
datalines;
1 
2
9
99
-99
-45
45
;

proc iml;
use example;
read all var _all_ into x[c=vname];
close;

x[loc(x=99 | x=-99)]=.;

create want from x[c=vname];
append from x;
close;
quit;
Rick_SAS
SAS Super FREQ

As Paige and KSharp said, use the LOC function. However, never use a "naked LOC":

always check that at least one observation satisfied the search criterion.

 

missingIdx = loc(m=-99 | m=99);
if ncol(missingIdx)>0 then
    m[where] = .;

 

Rick_SAS
SAS Super FREQ

I have merged the two separate threads into one thread.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 11 replies
  • 1755 views
  • 6 likes
  • 7 in conversation